eisa-bus.c 10 KB

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