lguest_bus.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <linux/init.h>
  2. #include <linux/bootmem.h>
  3. #include <linux/lguest_bus.h>
  4. #include <asm/io.h>
  5. static ssize_t type_show(struct device *_dev,
  6. struct device_attribute *attr, char *buf)
  7. {
  8. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  9. return sprintf(buf, "%hu", lguest_devices[dev->index].type);
  10. }
  11. static ssize_t features_show(struct device *_dev,
  12. struct device_attribute *attr, char *buf)
  13. {
  14. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  15. return sprintf(buf, "%hx", lguest_devices[dev->index].features);
  16. }
  17. static ssize_t pfn_show(struct device *_dev,
  18. struct device_attribute *attr, char *buf)
  19. {
  20. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  21. return sprintf(buf, "%u", lguest_devices[dev->index].pfn);
  22. }
  23. static ssize_t status_show(struct device *_dev,
  24. struct device_attribute *attr, char *buf)
  25. {
  26. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  27. return sprintf(buf, "%hx", lguest_devices[dev->index].status);
  28. }
  29. static ssize_t status_store(struct device *_dev, struct device_attribute *attr,
  30. const char *buf, size_t count)
  31. {
  32. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  33. if (sscanf(buf, "%hi", &lguest_devices[dev->index].status) != 1)
  34. return -EINVAL;
  35. return count;
  36. }
  37. static struct device_attribute lguest_dev_attrs[] = {
  38. __ATTR_RO(type),
  39. __ATTR_RO(features),
  40. __ATTR_RO(pfn),
  41. __ATTR(status, 0644, status_show, status_store),
  42. __ATTR_NULL
  43. };
  44. static int lguest_dev_match(struct device *_dev, struct device_driver *_drv)
  45. {
  46. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  47. struct lguest_driver *drv = container_of(_drv,struct lguest_driver,drv);
  48. return (drv->device_type == lguest_devices[dev->index].type);
  49. }
  50. struct lguest_bus {
  51. struct bus_type bus;
  52. struct device dev;
  53. };
  54. static struct lguest_bus lguest_bus = {
  55. .bus = {
  56. .name = "lguest",
  57. .match = lguest_dev_match,
  58. .dev_attrs = lguest_dev_attrs,
  59. },
  60. .dev = {
  61. .parent = NULL,
  62. .bus_id = "lguest",
  63. }
  64. };
  65. static int lguest_dev_probe(struct device *_dev)
  66. {
  67. int ret;
  68. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  69. struct lguest_driver *drv = container_of(dev->dev.driver,
  70. struct lguest_driver, drv);
  71. lguest_devices[dev->index].status |= LGUEST_DEVICE_S_DRIVER;
  72. ret = drv->probe(dev);
  73. if (ret == 0)
  74. lguest_devices[dev->index].status |= LGUEST_DEVICE_S_DRIVER_OK;
  75. return ret;
  76. }
  77. int register_lguest_driver(struct lguest_driver *drv)
  78. {
  79. if (!lguest_devices)
  80. return 0;
  81. drv->drv.bus = &lguest_bus.bus;
  82. drv->drv.name = drv->name;
  83. drv->drv.owner = drv->owner;
  84. drv->drv.probe = lguest_dev_probe;
  85. return driver_register(&drv->drv);
  86. }
  87. EXPORT_SYMBOL_GPL(register_lguest_driver);
  88. static void add_lguest_device(unsigned int index)
  89. {
  90. struct lguest_device *new;
  91. lguest_devices[index].status |= LGUEST_DEVICE_S_ACKNOWLEDGE;
  92. new = kmalloc(sizeof(struct lguest_device), GFP_KERNEL);
  93. if (!new) {
  94. printk(KERN_EMERG "Cannot allocate lguest device %u\n", index);
  95. lguest_devices[index].status |= LGUEST_DEVICE_S_FAILED;
  96. return;
  97. }
  98. new->index = index;
  99. new->private = NULL;
  100. memset(&new->dev, 0, sizeof(new->dev));
  101. new->dev.parent = &lguest_bus.dev;
  102. new->dev.bus = &lguest_bus.bus;
  103. sprintf(new->dev.bus_id, "%u", index);
  104. if (device_register(&new->dev) != 0) {
  105. printk(KERN_EMERG "Cannot register lguest device %u\n", index);
  106. lguest_devices[index].status |= LGUEST_DEVICE_S_FAILED;
  107. kfree(new);
  108. }
  109. }
  110. static void scan_devices(void)
  111. {
  112. unsigned int i;
  113. for (i = 0; i < LGUEST_MAX_DEVICES; i++)
  114. if (lguest_devices[i].type)
  115. add_lguest_device(i);
  116. }
  117. static int __init lguest_bus_init(void)
  118. {
  119. if (strcmp(paravirt_ops.name, "lguest") != 0)
  120. return 0;
  121. /* Devices are in page above top of "normal" mem. */
  122. lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);
  123. if (bus_register(&lguest_bus.bus) != 0
  124. || device_register(&lguest_bus.dev) != 0)
  125. panic("lguest bus registration failed");
  126. scan_devices();
  127. return 0;
  128. }
  129. postcore_initcall(lguest_bus_init);