virtio.c 5.8 KB

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