blob: 69d7ea02cd489bac96c3e3051678b669547e0211 [file] [log] [blame]
Rusty Russellec3d41c2007-10-22 11:03:36 +10001#include <linux/virtio.h>
2#include <linux/spinlock.h>
3#include <linux/virtio_config.h>
4
5static ssize_t device_show(struct device *_d,
6 struct device_attribute *attr, char *buf)
7{
8 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
9 return sprintf(buf, "%hu", dev->id.device);
10}
11static ssize_t vendor_show(struct device *_d,
12 struct device_attribute *attr, char *buf)
13{
14 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
15 return sprintf(buf, "%hu", dev->id.vendor);
16}
17static ssize_t status_show(struct device *_d,
18 struct device_attribute *attr, char *buf)
19{
20 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
21 return sprintf(buf, "0x%08x", dev->config->get_status(dev));
22}
Rusty Russellb01d9f22007-10-22 11:03:39 +100023static ssize_t modalias_show(struct device *_d,
24 struct device_attribute *attr, char *buf)
25{
26 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
27
28 return sprintf(buf, "virtio:d%08Xv%08X\n",
29 dev->id.device, dev->id.vendor);
30}
Rusty Russellec3d41c2007-10-22 11:03:36 +100031static struct device_attribute virtio_dev_attrs[] = {
32 __ATTR_RO(device),
33 __ATTR_RO(vendor),
34 __ATTR_RO(status),
Rusty Russellb01d9f22007-10-22 11:03:39 +100035 __ATTR_RO(modalias),
Rusty Russellec3d41c2007-10-22 11:03:36 +100036 __ATTR_NULL
37};
38
39static inline int virtio_id_match(const struct virtio_device *dev,
40 const struct virtio_device_id *id)
41{
42 if (id->device != dev->id.device)
43 return 0;
44
45 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor != dev->id.vendor;
46}
47
48/* This looks through all the IDs a driver claims to support. If any of them
49 * match, we return 1 and the kernel will call virtio_dev_probe(). */
50static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
51{
52 unsigned int i;
53 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
54 const struct virtio_device_id *ids;
55
56 ids = container_of(_dr, struct virtio_driver, driver)->id_table;
57 for (i = 0; ids[i].device; i++)
58 if (virtio_id_match(dev, &ids[i]))
59 return 1;
60 return 0;
61}
62
Rusty Russellb01d9f22007-10-22 11:03:39 +100063static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
64{
65 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
66
67 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
68 dev->id.device, dev->id.vendor);
69}
70
Rusty Russellec3d41c2007-10-22 11:03:36 +100071static struct bus_type virtio_bus = {
72 .name = "virtio",
73 .match = virtio_dev_match,
74 .dev_attrs = virtio_dev_attrs,
Rusty Russellb01d9f22007-10-22 11:03:39 +100075 .uevent = virtio_uevent,
Rusty Russellec3d41c2007-10-22 11:03:36 +100076};
77
78static void add_status(struct virtio_device *dev, unsigned status)
79{
80 dev->config->set_status(dev, dev->config->get_status(dev) | status);
81}
82
83static int virtio_dev_probe(struct device *_d)
84{
85 int err;
86 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
87 struct virtio_driver *drv = container_of(dev->dev.driver,
88 struct virtio_driver, driver);
89
90 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
91 err = drv->probe(dev);
92 if (err)
93 add_status(dev, VIRTIO_CONFIG_S_FAILED);
94 else
95 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
96 return err;
97}
98
Rusty Russell74b25532007-11-19 11:20:42 -050099static int virtio_dev_remove(struct device *_d)
100{
101 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
102 struct virtio_driver *drv = container_of(dev->dev.driver,
103 struct virtio_driver, driver);
104
105 dev->config->set_status(dev, dev->config->get_status(dev)
106 & ~VIRTIO_CONFIG_S_DRIVER);
107 drv->remove(dev);
108 return 0;
109}
110
Rusty Russellec3d41c2007-10-22 11:03:36 +1000111int register_virtio_driver(struct virtio_driver *driver)
112{
113 driver->driver.bus = &virtio_bus;
114 driver->driver.probe = virtio_dev_probe;
Rusty Russell74b25532007-11-19 11:20:42 -0500115 driver->driver.remove = virtio_dev_remove;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000116 return driver_register(&driver->driver);
117}
118EXPORT_SYMBOL_GPL(register_virtio_driver);
119
120void unregister_virtio_driver(struct virtio_driver *driver)
121{
122 driver_unregister(&driver->driver);
123}
124EXPORT_SYMBOL_GPL(unregister_virtio_driver);
125
126int register_virtio_device(struct virtio_device *dev)
127{
128 int err;
129
130 dev->dev.bus = &virtio_bus;
131 sprintf(dev->dev.bus_id, "%u", dev->index);
132
133 /* Acknowledge that we've seen the device. */
134 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
135
136 /* device_register() causes the bus infrastructure to look for a
137 * matching driver. */
138 err = device_register(&dev->dev);
139 if (err)
140 add_status(dev, VIRTIO_CONFIG_S_FAILED);
141 return err;
142}
143EXPORT_SYMBOL_GPL(register_virtio_device);
144
145void unregister_virtio_device(struct virtio_device *dev)
146{
147 device_unregister(&dev->dev);
148}
149EXPORT_SYMBOL_GPL(unregister_virtio_device);
150
151int __virtio_config_val(struct virtio_device *vdev,
152 u8 type, void *val, size_t size)
153{
154 void *token;
155 unsigned int len;
156
157 token = vdev->config->find(vdev, type, &len);
158 if (!token)
159 return -ENOENT;
160
161 if (len != size)
162 return -EIO;
163
164 vdev->config->get(vdev, token, val, size);
165 return 0;
166}
167EXPORT_SYMBOL_GPL(__virtio_config_val);
168
169int virtio_use_bit(struct virtio_device *vdev,
170 void *token, unsigned int len, unsigned int bitnum)
171{
172 unsigned long bits[16];
173
174 /* This makes it convenient to pass-through find() results. */
175 if (!token)
176 return 0;
177
178 /* bit not in range of this bitfield? */
179 if (bitnum * 8 >= len / 2)
180 return 0;
181
182 /* Giant feature bitfields are silly. */
183 BUG_ON(len > sizeof(bits));
184 vdev->config->get(vdev, token, bits, len);
185
186 if (!test_bit(bitnum, bits))
187 return 0;
188
189 /* Set acknowledge bit, and write it back. */
190 set_bit(bitnum + len * 8 / 2, bits);
191 vdev->config->set(vdev, token, bits, len);
192 return 1;
193}
194EXPORT_SYMBOL_GPL(virtio_use_bit);
195
196static int virtio_init(void)
197{
198 if (bus_register(&virtio_bus) != 0)
199 panic("virtio bus registration failed");
200 return 0;
201}
202core_initcall(virtio_init);