1 #include <linux/virtio.h>
2 #include <linux/spinlock.h>
3 #include <linux/virtio_config.h>
5 static ssize_t device_show(struct device *_d,
6 struct device_attribute *attr, char *buf)
8 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
9 return sprintf(buf, "%hu", dev->id.device);
11 static ssize_t vendor_show(struct device *_d,
12 struct device_attribute *attr, char *buf)
14 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
15 return sprintf(buf, "%hu", dev->id.vendor);
17 static ssize_t status_show(struct device *_d,
18 struct device_attribute *attr, char *buf)
20 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
21 return sprintf(buf, "0x%08x", dev->config->get_status(dev));
23 static struct device_attribute virtio_dev_attrs[] = {
30 static inline int virtio_id_match(const struct virtio_device *dev,
31 const struct virtio_device_id *id)
33 if (id->device != dev->id.device)
36 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor != dev->id.vendor;
39 /* This looks through all the IDs a driver claims to support. If any of them
40 * match, we return 1 and the kernel will call virtio_dev_probe(). */
41 static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
44 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
45 const struct virtio_device_id *ids;
47 ids = container_of(_dr, struct virtio_driver, driver)->id_table;
48 for (i = 0; ids[i].device; i++)
49 if (virtio_id_match(dev, &ids[i]))
54 static struct bus_type virtio_bus = {
56 .match = virtio_dev_match,
57 .dev_attrs = virtio_dev_attrs,
60 static void add_status(struct virtio_device *dev, unsigned status)
62 dev->config->set_status(dev, dev->config->get_status(dev) | status);
65 static int virtio_dev_probe(struct device *_d)
68 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
69 struct virtio_driver *drv = container_of(dev->dev.driver,
70 struct virtio_driver, driver);
72 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
73 err = drv->probe(dev);
75 add_status(dev, VIRTIO_CONFIG_S_FAILED);
77 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
81 int register_virtio_driver(struct virtio_driver *driver)
83 driver->driver.bus = &virtio_bus;
84 driver->driver.probe = virtio_dev_probe;
85 return driver_register(&driver->driver);
87 EXPORT_SYMBOL_GPL(register_virtio_driver);
89 void unregister_virtio_driver(struct virtio_driver *driver)
91 driver_unregister(&driver->driver);
93 EXPORT_SYMBOL_GPL(unregister_virtio_driver);
95 int register_virtio_device(struct virtio_device *dev)
99 dev->dev.bus = &virtio_bus;
100 sprintf(dev->dev.bus_id, "%u", dev->index);
102 /* Acknowledge that we've seen the device. */
103 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
105 /* device_register() causes the bus infrastructure to look for a
106 * matching driver. */
107 err = device_register(&dev->dev);
109 add_status(dev, VIRTIO_CONFIG_S_FAILED);
112 EXPORT_SYMBOL_GPL(register_virtio_device);
114 void unregister_virtio_device(struct virtio_device *dev)
116 device_unregister(&dev->dev);
118 EXPORT_SYMBOL_GPL(unregister_virtio_device);
120 int __virtio_config_val(struct virtio_device *vdev,
121 u8 type, void *val, size_t size)
126 token = vdev->config->find(vdev, type, &len);
133 vdev->config->get(vdev, token, val, size);
136 EXPORT_SYMBOL_GPL(__virtio_config_val);
138 int virtio_use_bit(struct virtio_device *vdev,
139 void *token, unsigned int len, unsigned int bitnum)
141 unsigned long bits[16];
143 /* This makes it convenient to pass-through find() results. */
147 /* bit not in range of this bitfield? */
148 if (bitnum * 8 >= len / 2)
151 /* Giant feature bitfields are silly. */
152 BUG_ON(len > sizeof(bits));
153 vdev->config->get(vdev, token, bits, len);
155 if (!test_bit(bitnum, bits))
158 /* Set acknowledge bit, and write it back. */
159 set_bit(bitnum + len * 8 / 2, bits);
160 vdev->config->set(vdev, token, bits, len);
163 EXPORT_SYMBOL_GPL(virtio_use_bit);
165 static int virtio_init(void)
167 if (bus_register(&virtio_bus) != 0)
168 panic("virtio bus registration failed");
171 core_initcall(virtio_init);