eisa-bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * EISA bus support functions for sysfs.
  3. *
  4. * (C) 2002, 2003 Marc Zyngier <maz@wild-wind.fr.eu.org>
  5. *
  6. * This code is released under the GPL version 2.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/eisa.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/ioport.h>
  16. #include <asm/io.h>
  17. #define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))
  18. #define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }
  19. struct eisa_device_info {
  20. struct eisa_device_id id;
  21. char name[50];
  22. };
  23. #ifdef CONFIG_EISA_NAMES
  24. static struct eisa_device_info __initdata eisa_table[] = {
  25. #include "devlist.h"
  26. };
  27. #define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
  28. #endif
  29. #define EISA_MAX_FORCED_DEV 16
  30. static int enable_dev[EISA_MAX_FORCED_DEV];
  31. static unsigned int enable_dev_count;
  32. static int disable_dev[EISA_MAX_FORCED_DEV];
  33. static unsigned int disable_dev_count;
  34. static int is_forced_dev(int *forced_tab,
  35. int forced_count,
  36. struct eisa_root_device *root,
  37. struct eisa_device *edev)
  38. {
  39. int i, x;
  40. for (i = 0; i < forced_count; i++) {
  41. x = (root->bus_nr << 8) | edev->slot;
  42. if (forced_tab[i] == x)
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. static void __init eisa_name_device(struct eisa_device *edev)
  48. {
  49. #ifdef CONFIG_EISA_NAMES
  50. int i;
  51. for (i = 0; i < EISA_INFOS; i++) {
  52. if (!strcmp(edev->id.sig, eisa_table[i].id.sig)) {
  53. strlcpy(edev->pretty_name,
  54. eisa_table[i].name,
  55. sizeof(edev->pretty_name));
  56. return;
  57. }
  58. }
  59. /* No name was found */
  60. sprintf(edev->pretty_name, "EISA device %.7s", edev->id.sig);
  61. #endif
  62. }
  63. static char __init *decode_eisa_sig(unsigned long addr)
  64. {
  65. static char sig_str[EISA_SIG_LEN];
  66. u8 sig[4];
  67. u16 rev;
  68. int i;
  69. for (i = 0; i < 4; i++) {
  70. #ifdef CONFIG_EISA_VLB_PRIMING
  71. /*
  72. * This ugly stuff is used to wake up VL-bus cards
  73. * (AHA-284x is the only known example), so we can
  74. * read the EISA id.
  75. *
  76. * Thankfully, this only exists on x86...
  77. */
  78. outb(0x80 + i, addr);
  79. #endif
  80. sig[i] = inb(addr + i);
  81. if (!i && (sig[0] & 0x80))
  82. return NULL;
  83. }
  84. sig_str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1);
  85. sig_str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1);
  86. sig_str[2] = (sig[1] & 0x1f) + ('A' - 1);
  87. rev = (sig[2] << 8) | sig[3];
  88. sprintf(sig_str + 3, "%04X", rev);
  89. return sig_str;
  90. }
  91. static int eisa_bus_match(struct device *dev, struct device_driver *drv)
  92. {
  93. struct eisa_device *edev = to_eisa_device(dev);
  94. struct eisa_driver *edrv = to_eisa_driver(drv);
  95. const struct eisa_device_id *eids = edrv->id_table;
  96. if (!eids)
  97. return 0;
  98. while (strlen(eids->sig)) {
  99. if (!strcmp(eids->sig, edev->id.sig) &&
  100. edev->state & EISA_CONFIG_ENABLED) {
  101. edev->id.driver_data = eids->driver_data;
  102. return 1;
  103. }
  104. eids++;
  105. }
  106. return 0;
  107. }
  108. static int eisa_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  109. {
  110. struct eisa_device *edev = to_eisa_device(dev);
  111. add_uevent_var(env, "MODALIAS=" EISA_DEVICE_MODALIAS_FMT, edev->id.sig);
  112. return 0;
  113. }
  114. struct bus_type eisa_bus_type = {
  115. .name = "eisa",
  116. .match = eisa_bus_match,
  117. .uevent = eisa_bus_uevent,
  118. };
  119. EXPORT_SYMBOL(eisa_bus_type);
  120. int eisa_driver_register(struct eisa_driver *edrv)
  121. {
  122. edrv->driver.bus = &eisa_bus_type;
  123. return driver_register(&edrv->driver);
  124. }
  125. EXPORT_SYMBOL(eisa_driver_register);
  126. void eisa_driver_unregister(struct eisa_driver *edrv)
  127. {
  128. driver_unregister(&edrv->driver);
  129. }
  130. EXPORT_SYMBOL(eisa_driver_unregister);
  131. static ssize_t eisa_show_sig(struct device *dev, struct device_attribute *attr,
  132. char *buf)
  133. {
  134. struct eisa_device *edev = to_eisa_device(dev);
  135. return sprintf(buf, "%s\n", edev->id.sig);
  136. }
  137. static DEVICE_ATTR(signature, S_IRUGO, eisa_show_sig, NULL);
  138. static ssize_t eisa_show_state(struct device *dev,
  139. struct device_attribute *attr,
  140. char *buf)
  141. {
  142. struct eisa_device *edev = to_eisa_device(dev);
  143. return sprintf(buf, "%d\n", edev->state & EISA_CONFIG_ENABLED);
  144. }
  145. static DEVICE_ATTR(enabled, S_IRUGO, eisa_show_state, NULL);
  146. static ssize_t eisa_show_modalias(struct device *dev,
  147. struct device_attribute *attr,
  148. char *buf)
  149. {
  150. struct eisa_device *edev = to_eisa_device(dev);
  151. return sprintf(buf, EISA_DEVICE_MODALIAS_FMT "\n", edev->id.sig);
  152. }
  153. static DEVICE_ATTR(modalias, S_IRUGO, eisa_show_modalias, NULL);
  154. static int __init eisa_init_device(struct eisa_root_device *root,
  155. struct eisa_device *edev,
  156. int slot)
  157. {
  158. char *sig;
  159. unsigned long sig_addr;
  160. int i;
  161. sig_addr = SLOT_ADDRESS(root, slot) + EISA_VENDOR_ID_OFFSET;
  162. sig = decode_eisa_sig(sig_addr);
  163. if (!sig)
  164. return -1; /* No EISA device here */
  165. memcpy(edev->id.sig, sig, EISA_SIG_LEN);
  166. edev->slot = slot;
  167. edev->state = inb(SLOT_ADDRESS(root, slot) + EISA_CONFIG_OFFSET)
  168. & EISA_CONFIG_ENABLED;
  169. edev->base_addr = SLOT_ADDRESS(root, slot);
  170. edev->dma_mask = root->dma_mask; /* Default DMA mask */
  171. eisa_name_device(edev);
  172. edev->dev.parent = root->dev;
  173. edev->dev.bus = &eisa_bus_type;
  174. edev->dev.dma_mask = &edev->dma_mask;
  175. edev->dev.coherent_dma_mask = edev->dma_mask;
  176. dev_set_name(&edev->dev, "%02X:%02X", root->bus_nr, slot);
  177. for (i = 0; i < EISA_MAX_RESOURCES; i++) {
  178. #ifdef CONFIG_EISA_NAMES
  179. edev->res[i].name = edev->pretty_name;
  180. #else
  181. edev->res[i].name = edev->id.sig;
  182. #endif
  183. }
  184. if (is_forced_dev(enable_dev, enable_dev_count, root, edev))
  185. edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
  186. if (is_forced_dev(disable_dev, disable_dev_count, root, edev))
  187. edev->state = EISA_CONFIG_FORCED;
  188. return 0;
  189. }
  190. static int __init eisa_register_device(struct eisa_device *edev)
  191. {
  192. int rc = device_register(&edev->dev);
  193. if (rc) {
  194. put_device(&edev->dev);
  195. return rc;
  196. }
  197. rc = device_create_file(&edev->dev, &dev_attr_signature);
  198. if (rc)
  199. goto err_devreg;
  200. rc = device_create_file(&edev->dev, &dev_attr_enabled);
  201. if (rc)
  202. goto err_sig;
  203. rc = device_create_file(&edev->dev, &dev_attr_modalias);
  204. if (rc)
  205. goto err_enab;
  206. return 0;
  207. err_enab:
  208. device_remove_file(&edev->dev, &dev_attr_enabled);
  209. err_sig:
  210. device_remove_file(&edev->dev, &dev_attr_signature);
  211. err_devreg:
  212. device_unregister(&edev->dev);
  213. return rc;
  214. }
  215. static int __init eisa_request_resources(struct eisa_root_device *root,
  216. struct eisa_device *edev,
  217. int slot)
  218. {
  219. int i;
  220. for (i = 0; i < EISA_MAX_RESOURCES; i++) {
  221. /* Don't register resource for slot 0, since this is
  222. * very likely to fail... :-( Instead, grab the EISA
  223. * id, now we can display something in /proc/ioports.
  224. */
  225. /* Only one region for mainboard */
  226. if (!slot && i > 0) {
  227. edev->res[i].start = edev->res[i].end = 0;
  228. continue;
  229. }
  230. if (slot) {
  231. edev->res[i].name = NULL;
  232. edev->res[i].start = SLOT_ADDRESS(root, slot)
  233. + (i * 0x400);
  234. edev->res[i].end = edev->res[i].start + 0xff;
  235. edev->res[i].flags = IORESOURCE_IO;
  236. } else {
  237. edev->res[i].name = NULL;
  238. edev->res[i].start = SLOT_ADDRESS(root, slot)
  239. + EISA_VENDOR_ID_OFFSET;
  240. edev->res[i].end = edev->res[i].start + 3;
  241. edev->res[i].flags = IORESOURCE_IO | IORESOURCE_BUSY;
  242. }
  243. if (request_resource(root->res, &edev->res[i]))
  244. goto failed;
  245. }
  246. return 0;
  247. failed:
  248. while (--i >= 0)
  249. release_resource(&edev->res[i]);
  250. return -1;
  251. }
  252. static void __init eisa_release_resources(struct eisa_device *edev)
  253. {
  254. int i;
  255. for (i = 0; i < EISA_MAX_RESOURCES; i++)
  256. if (edev->res[i].start || edev->res[i].end)
  257. release_resource(&edev->res[i]);
  258. }
  259. static int __init eisa_probe(struct eisa_root_device *root)
  260. {
  261. int i, c;
  262. struct eisa_device *edev;
  263. char *enabled_str;
  264. dev_info(root->dev, "Probing EISA bus %d\n", root->bus_nr);
  265. /* First try to get hold of slot 0. If there is no device
  266. * here, simply fail, unless root->force_probe is set. */
  267. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  268. if (!edev) {
  269. dev_err(root->dev, "EISA: Couldn't allocate mainboard slot\n");
  270. return -ENOMEM;
  271. }
  272. if (eisa_request_resources(root, edev, 0)) {
  273. dev_warn(root->dev,
  274. "EISA: Cannot allocate resource for mainboard\n");
  275. kfree(edev);
  276. if (!root->force_probe)
  277. return -EBUSY;
  278. goto force_probe;
  279. }
  280. if (eisa_init_device(root, edev, 0)) {
  281. eisa_release_resources(edev);
  282. kfree(edev);
  283. if (!root->force_probe)
  284. return -ENODEV;
  285. goto force_probe;
  286. }
  287. dev_info(&edev->dev, "EISA: Mainboard %s detected\n", edev->id.sig);
  288. if (eisa_register_device(edev)) {
  289. dev_err(&edev->dev, "EISA: Failed to register %s\n",
  290. edev->id.sig);
  291. eisa_release_resources(edev);
  292. kfree(edev);
  293. }
  294. force_probe:
  295. for (c = 0, i = 1; i <= root->slots; i++) {
  296. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  297. if (!edev) {
  298. dev_err(root->dev, "EISA: Out of memory for slot %d\n",
  299. i);
  300. continue;
  301. }
  302. if (eisa_request_resources(root, edev, i)) {
  303. dev_warn(root->dev,
  304. "Cannot allocate resource for EISA slot %d\n",
  305. i);
  306. kfree(edev);
  307. continue;
  308. }
  309. if (eisa_init_device(root, edev, i)) {
  310. eisa_release_resources(edev);
  311. kfree(edev);
  312. continue;
  313. }
  314. if (edev->state == (EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED))
  315. enabled_str = " (forced enabled)";
  316. else if (edev->state == EISA_CONFIG_FORCED)
  317. enabled_str = " (forced disabled)";
  318. else if (edev->state == 0)
  319. enabled_str = " (disabled)";
  320. else
  321. enabled_str = "";
  322. dev_info(&edev->dev, "EISA: slot %d: %s detected%s\n", i,
  323. edev->id.sig, enabled_str);
  324. c++;
  325. if (eisa_register_device(edev)) {
  326. dev_err(&edev->dev, "EISA: Failed to register %s\n",
  327. edev->id.sig);
  328. eisa_release_resources(edev);
  329. kfree(edev);
  330. }
  331. }
  332. dev_info(root->dev, "EISA: Detected %d card%s\n", c, c == 1 ? "" : "s");
  333. return 0;
  334. }
  335. static struct resource eisa_root_res = {
  336. .name = "EISA root resource",
  337. .start = 0,
  338. .end = 0xffffffff,
  339. .flags = IORESOURCE_IO,
  340. };
  341. static int eisa_bus_count;
  342. int __init eisa_root_register(struct eisa_root_device *root)
  343. {
  344. int err;
  345. /* Use our own resources to check if this bus base address has
  346. * been already registered. This prevents the virtual root
  347. * device from registering after the real one has, for
  348. * example... */
  349. root->eisa_root_res.name = eisa_root_res.name;
  350. root->eisa_root_res.start = root->res->start;
  351. root->eisa_root_res.end = root->res->end;
  352. root->eisa_root_res.flags = IORESOURCE_BUSY;
  353. err = request_resource(&eisa_root_res, &root->eisa_root_res);
  354. if (err)
  355. return err;
  356. root->bus_nr = eisa_bus_count++;
  357. err = eisa_probe(root);
  358. if (err)
  359. release_resource(&root->eisa_root_res);
  360. return err;
  361. }
  362. static int __init eisa_init(void)
  363. {
  364. int r;
  365. r = bus_register(&eisa_bus_type);
  366. if (r)
  367. return r;
  368. printk(KERN_INFO "EISA bus registered\n");
  369. return 0;
  370. }
  371. module_param_array(enable_dev, int, &enable_dev_count, 0444);
  372. module_param_array(disable_dev, int, &disable_dev_count, 0444);
  373. postcore_initcall(eisa_init);
  374. int EISA_bus; /* for legacy drivers */
  375. EXPORT_SYMBOL(EISA_bus);