virtio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <linux/virtio.h>
  2. #include <linux/spinlock.h>
  3. #include <linux/virtio_config.h>
  4. static ssize_t device_show(struct device *_d,
  5. struct device_attribute *attr, char *buf)
  6. {
  7. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  8. return sprintf(buf, "%hu", dev->id.device);
  9. }
  10. static ssize_t vendor_show(struct device *_d,
  11. struct device_attribute *attr, char *buf)
  12. {
  13. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  14. return sprintf(buf, "%hu", dev->id.vendor);
  15. }
  16. static ssize_t status_show(struct device *_d,
  17. struct device_attribute *attr, char *buf)
  18. {
  19. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  20. return sprintf(buf, "0x%08x", dev->config->get_status(dev));
  21. }
  22. static ssize_t modalias_show(struct device *_d,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  26. return sprintf(buf, "virtio:d%08Xv%08X\n",
  27. dev->id.device, dev->id.vendor);
  28. }
  29. static struct device_attribute virtio_dev_attrs[] = {
  30. __ATTR_RO(device),
  31. __ATTR_RO(vendor),
  32. __ATTR_RO(status),
  33. __ATTR_RO(modalias),
  34. __ATTR_NULL
  35. };
  36. static inline int virtio_id_match(const struct virtio_device *dev,
  37. const struct virtio_device_id *id)
  38. {
  39. if (id->device != dev->id.device)
  40. return 0;
  41. return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor != dev->id.vendor;
  42. }
  43. /* This looks through all the IDs a driver claims to support. If any of them
  44. * match, we return 1 and the kernel will call virtio_dev_probe(). */
  45. static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
  46. {
  47. unsigned int i;
  48. struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
  49. const struct virtio_device_id *ids;
  50. ids = container_of(_dr, struct virtio_driver, driver)->id_table;
  51. for (i = 0; ids[i].device; i++)
  52. if (virtio_id_match(dev, &ids[i]))
  53. return 1;
  54. return 0;
  55. }
  56. static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
  57. {
  58. struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
  59. return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
  60. dev->id.device, dev->id.vendor);
  61. }
  62. static struct bus_type virtio_bus = {
  63. .name = "virtio",
  64. .match = virtio_dev_match,
  65. .dev_attrs = virtio_dev_attrs,
  66. .uevent = virtio_uevent,
  67. };
  68. static void add_status(struct virtio_device *dev, unsigned status)
  69. {
  70. dev->config->set_status(dev, dev->config->get_status(dev) | status);
  71. }
  72. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  73. unsigned int fbit)
  74. {
  75. unsigned int i;
  76. struct virtio_driver *drv = container_of(vdev->dev.driver,
  77. struct virtio_driver, driver);
  78. for (i = 0; i < drv->feature_table_size; i++)
  79. if (drv->feature_table[i] == fbit)
  80. return;
  81. BUG();
  82. }
  83. EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
  84. static int virtio_dev_probe(struct device *_d)
  85. {
  86. int err, i;
  87. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  88. struct virtio_driver *drv = container_of(dev->dev.driver,
  89. struct virtio_driver, driver);
  90. u32 device_features;
  91. /* We have a driver! */
  92. add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  93. /* Figure out what features the device supports. */
  94. device_features = dev->config->get_features(dev);
  95. /* Features supported by both device and driver into dev->features. */
  96. memset(dev->features, 0, sizeof(dev->features));
  97. for (i = 0; i < drv->feature_table_size; i++) {
  98. unsigned int f = drv->feature_table[i];
  99. BUG_ON(f >= 32);
  100. if (device_features & (1 << f))
  101. set_bit(f, dev->features);
  102. }
  103. err = drv->probe(dev);
  104. if (err)
  105. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  106. else {
  107. add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
  108. /* They should never have set feature bits beyond 32 */
  109. dev->config->set_features(dev, dev->features[0]);
  110. }
  111. return err;
  112. }
  113. static int virtio_dev_remove(struct device *_d)
  114. {
  115. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  116. struct virtio_driver *drv = container_of(dev->dev.driver,
  117. struct virtio_driver, driver);
  118. drv->remove(dev);
  119. /* Driver should have reset device. */
  120. BUG_ON(dev->config->get_status(dev));
  121. /* Acknowledge the device's existence again. */
  122. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  123. return 0;
  124. }
  125. int register_virtio_driver(struct virtio_driver *driver)
  126. {
  127. /* Catch this early. */
  128. BUG_ON(driver->feature_table_size && !driver->feature_table);
  129. driver->driver.bus = &virtio_bus;
  130. driver->driver.probe = virtio_dev_probe;
  131. driver->driver.remove = virtio_dev_remove;
  132. return driver_register(&driver->driver);
  133. }
  134. EXPORT_SYMBOL_GPL(register_virtio_driver);
  135. void unregister_virtio_driver(struct virtio_driver *driver)
  136. {
  137. driver_unregister(&driver->driver);
  138. }
  139. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  140. int register_virtio_device(struct virtio_device *dev)
  141. {
  142. int err;
  143. dev->dev.bus = &virtio_bus;
  144. sprintf(dev->dev.bus_id, "%u", dev->index);
  145. /* We always start by resetting the device, in case a previous
  146. * driver messed it up. This also tests that code path a little. */
  147. dev->config->reset(dev);
  148. /* Acknowledge that we've seen the device. */
  149. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  150. /* device_register() causes the bus infrastructure to look for a
  151. * matching driver. */
  152. err = device_register(&dev->dev);
  153. if (err)
  154. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  155. return err;
  156. }
  157. EXPORT_SYMBOL_GPL(register_virtio_device);
  158. void unregister_virtio_device(struct virtio_device *dev)
  159. {
  160. device_unregister(&dev->dev);
  161. }
  162. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  163. static int virtio_init(void)
  164. {
  165. if (bus_register(&virtio_bus) != 0)
  166. panic("virtio bus registration failed");
  167. return 0;
  168. }
  169. static void __exit virtio_exit(void)
  170. {
  171. bus_unregister(&virtio_bus);
  172. }
  173. core_initcall(virtio_init);
  174. module_exit(virtio_exit);
  175. MODULE_LICENSE("GPL");