PageRenderTime 48ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/mca/mca-bus.c

https://bitbucket.org/ndreys/linux-sunxi
C | 169 lines | 109 code | 25 blank | 35 comment | 15 complexity | bb83224ed1680fd85aac09dfdd0dd90f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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[50];
  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 unsigned short *mca_ids = mca_drv->id_table;
  45. int i = 0;
  46. if (mca_ids) {
  47. for(i = 0; mca_ids[i]; i++) {
  48. if (mca_ids[i] == mca_dev->pos_id) {
  49. mca_dev->index = i;
  50. return 1;
  51. }
  52. }
  53. }
  54. /* If the integrated id is present, treat it as though it were an
  55. * additional id in the id_table (it can't be because by definition,
  56. * integrated id's overflow a short */
  57. if (mca_drv->integrated_id && mca_dev->pos_id ==
  58. mca_drv->integrated_id) {
  59. mca_dev->index = i;
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. struct bus_type mca_bus_type = {
  65. .name = "MCA",
  66. .match = mca_bus_match,
  67. };
  68. EXPORT_SYMBOL (mca_bus_type);
  69. static ssize_t mca_show_pos_id(struct device *dev, struct device_attribute *attr, char *buf)
  70. {
  71. /* four digits, \n and trailing \0 */
  72. struct mca_device *mca_dev = to_mca_device(dev);
  73. int len;
  74. if(mca_dev->pos_id < MCA_DUMMY_POS_START)
  75. len = sprintf(buf, "%04x\n", mca_dev->pos_id);
  76. else
  77. len = sprintf(buf, "none\n");
  78. return len;
  79. }
  80. static ssize_t mca_show_pos(struct device *dev, struct device_attribute *attr, char *buf)
  81. {
  82. /* enough for 8 two byte hex chars plus space and new line */
  83. int j, len=0;
  84. struct mca_device *mca_dev = to_mca_device(dev);
  85. for(j=0; j<8; j++)
  86. len += sprintf(buf+len, "%02x ", mca_dev->pos[j]);
  87. /* change last trailing space to new line */
  88. buf[len-1] = '\n';
  89. return len;
  90. }
  91. static DEVICE_ATTR(id, S_IRUGO, mca_show_pos_id, NULL);
  92. static DEVICE_ATTR(pos, S_IRUGO, mca_show_pos, NULL);
  93. int __init mca_register_device(int bus, struct mca_device *mca_dev)
  94. {
  95. struct mca_bus *mca_bus = mca_root_busses[bus];
  96. int rc;
  97. mca_dev->dev.parent = &mca_bus->dev;
  98. mca_dev->dev.bus = &mca_bus_type;
  99. dev_set_name(&mca_dev->dev, "%02d:%02X", bus, mca_dev->slot);
  100. mca_dev->dma_mask = mca_bus->default_dma_mask;
  101. mca_dev->dev.dma_mask = &mca_dev->dma_mask;
  102. mca_dev->dev.coherent_dma_mask = mca_dev->dma_mask;
  103. rc = device_register(&mca_dev->dev);
  104. if (rc)
  105. goto err_out;
  106. rc = device_create_file(&mca_dev->dev, &dev_attr_id);
  107. if (rc) goto err_out_devreg;
  108. rc = device_create_file(&mca_dev->dev, &dev_attr_pos);
  109. if (rc) goto err_out_id;
  110. return 1;
  111. err_out_id:
  112. device_remove_file(&mca_dev->dev, &dev_attr_id);
  113. err_out_devreg:
  114. device_unregister(&mca_dev->dev);
  115. err_out:
  116. return 0;
  117. }
  118. /* */
  119. struct mca_bus * __devinit mca_attach_bus(int bus)
  120. {
  121. struct mca_bus *mca_bus;
  122. if (unlikely(mca_root_busses[bus] != NULL)) {
  123. /* This should never happen, but just in case */
  124. printk(KERN_EMERG "MCA tried to add already existing bus %d\n",
  125. bus);
  126. dump_stack();
  127. return NULL;
  128. }
  129. mca_bus = kzalloc(sizeof(struct mca_bus), GFP_KERNEL);
  130. if (!mca_bus)
  131. return NULL;
  132. dev_set_name(&mca_bus->dev, "mca%d", bus);
  133. sprintf(mca_bus->name,"Host %s MCA Bridge", bus ? "Secondary" : "Primary");
  134. if (device_register(&mca_bus->dev)) {
  135. kfree(mca_bus);
  136. return NULL;
  137. }
  138. mca_root_busses[bus] = mca_bus;
  139. return mca_bus;
  140. }
  141. int __init mca_system_init (void)
  142. {
  143. return bus_register(&mca_bus_type);
  144. }