mca-bus.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /*
  3. * MCA bus support functions for sysfs.
  4. *
  5. * (C) 2002 James Bottomley <James.Bottomley@HansenPartnership.com>
  6. *
  7. **-----------------------------------------------------------------------------
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. **
  23. **-----------------------------------------------------------------------------
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/device.h>
  27. #include <linux/mca.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. /* Very few machines have more than one MCA bus. However, there are
  32. * those that do (Voyager 35xx/5xxx), so we do it this way for future
  33. * expansion. None that I know have more than 2 */
  34. static struct mca_bus *mca_root_busses[MAX_MCA_BUSSES];
  35. #define MCA_DEVINFO(i,s) { .pos = i, .name = s }
  36. struct mca_device_info {
  37. short pos_id; /* the 2 byte pos id for this card */
  38. char name[DEVICE_NAME_SIZE];
  39. };
  40. static int mca_bus_match (struct device *dev, struct device_driver *drv)
  41. {
  42. struct mca_device *mca_dev = to_mca_device (dev);
  43. struct mca_driver *mca_drv = to_mca_driver (drv);
  44. const short *mca_ids = mca_drv->id_table;
  45. int i;
  46. if (!mca_ids)
  47. return 0;
  48. for(i = 0; mca_ids[i]; i++) {
  49. if (mca_ids[i] == mca_dev->pos_id) {
  50. mca_dev->index = i;
  51. return 1;
  52. }
  53. }
  54. return 0;
  55. }
  56. struct bus_type mca_bus_type = {
  57. .name = "MCA",
  58. .match = mca_bus_match,
  59. };
  60. EXPORT_SYMBOL (mca_bus_type);
  61. static ssize_t mca_show_pos_id(struct device *dev, struct device_attribute *attr, char *buf)
  62. {
  63. /* four digits, \n and trailing \0 */
  64. struct mca_device *mca_dev = to_mca_device(dev);
  65. int len;
  66. if(mca_dev->pos_id < MCA_DUMMY_POS_START)
  67. len = sprintf(buf, "%04x\n", mca_dev->pos_id);
  68. else
  69. len = sprintf(buf, "none\n");
  70. return len;
  71. }
  72. static ssize_t mca_show_pos(struct device *dev, struct device_attribute *attr, char *buf)
  73. {
  74. /* enough for 8 two byte hex chars plus space and new line */
  75. int j, len=0;
  76. struct mca_device *mca_dev = to_mca_device(dev);
  77. for(j=0; j<8; j++)
  78. len += sprintf(buf+len, "%02x ", mca_dev->pos[j]);
  79. /* change last trailing space to new line */
  80. buf[len-1] = '\n';
  81. return len;
  82. }
  83. static DEVICE_ATTR(id, S_IRUGO, mca_show_pos_id, NULL);
  84. static DEVICE_ATTR(pos, S_IRUGO, mca_show_pos, NULL);
  85. int __init mca_register_device(int bus, struct mca_device *mca_dev)
  86. {
  87. struct mca_bus *mca_bus = mca_root_busses[bus];
  88. mca_dev->dev.parent = &mca_bus->dev;
  89. mca_dev->dev.bus = &mca_bus_type;
  90. sprintf (mca_dev->dev.bus_id, "%02d:%02X", bus, mca_dev->slot);
  91. mca_dev->dma_mask = mca_bus->default_dma_mask;
  92. mca_dev->dev.dma_mask = &mca_dev->dma_mask;
  93. mca_dev->dev.coherent_dma_mask = mca_dev->dma_mask;
  94. if (device_register(&mca_dev->dev))
  95. return 0;
  96. device_create_file(&mca_dev->dev, &dev_attr_id);
  97. device_create_file(&mca_dev->dev, &dev_attr_pos);
  98. return 1;
  99. }
  100. /* */
  101. struct mca_bus * __devinit mca_attach_bus(int bus)
  102. {
  103. struct mca_bus *mca_bus;
  104. if (unlikely(mca_root_busses[bus] != NULL)) {
  105. /* This should never happen, but just in case */
  106. printk(KERN_EMERG "MCA tried to add already existing bus %d\n",
  107. bus);
  108. dump_stack();
  109. return NULL;
  110. }
  111. mca_bus = kmalloc(sizeof(struct mca_bus), GFP_KERNEL);
  112. if (!mca_bus)
  113. return NULL;
  114. memset(mca_bus, 0, sizeof(struct mca_bus));
  115. sprintf(mca_bus->dev.bus_id,"mca%d",bus);
  116. sprintf(mca_bus->name,"Host %s MCA Bridge", bus ? "Secondary" : "Primary");
  117. device_register(&mca_bus->dev);
  118. mca_root_busses[bus] = mca_bus;
  119. return mca_bus;
  120. }
  121. int __init mca_system_init (void)
  122. {
  123. return bus_register(&mca_bus_type);
  124. }