Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 1 | #include <linux/virtio.h> |
| 2 | #include <linux/spinlock.h> |
| 3 | #include <linux/virtio_config.h> |
| 4 | |
| 5 | static 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 | } |
| 11 | static 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 | } |
| 17 | static 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 Russell | b01d9f2 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 23 | static 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 Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 31 | static struct device_attribute virtio_dev_attrs[] = { |
| 32 | __ATTR_RO(device), |
| 33 | __ATTR_RO(vendor), |
| 34 | __ATTR_RO(status), |
Rusty Russell | b01d9f2 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 35 | __ATTR_RO(modalias), |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 36 | __ATTR_NULL |
| 37 | }; |
| 38 | |
| 39 | static 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(). */ |
| 50 | static 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 Russell | b01d9f2 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 63 | static 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 Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 71 | static struct bus_type virtio_bus = { |
| 72 | .name = "virtio", |
| 73 | .match = virtio_dev_match, |
| 74 | .dev_attrs = virtio_dev_attrs, |
Rusty Russell | b01d9f2 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 75 | .uevent = virtio_uevent, |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static void add_status(struct virtio_device *dev, unsigned status) |
| 79 | { |
| 80 | dev->config->set_status(dev, dev->config->get_status(dev) | status); |
| 81 | } |
| 82 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 83 | void virtio_check_driver_offered_feature(const struct virtio_device *vdev, |
| 84 | unsigned int fbit) |
| 85 | { |
| 86 | unsigned int i; |
| 87 | struct virtio_driver *drv = container_of(vdev->dev.driver, |
| 88 | struct virtio_driver, driver); |
| 89 | |
| 90 | for (i = 0; i < drv->feature_table_size; i++) |
| 91 | if (drv->feature_table[i] == fbit) |
| 92 | return; |
| 93 | BUG(); |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature); |
| 96 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 97 | static int virtio_dev_probe(struct device *_d) |
| 98 | { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 99 | int err, i; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 100 | struct virtio_device *dev = container_of(_d,struct virtio_device,dev); |
| 101 | struct virtio_driver *drv = container_of(dev->dev.driver, |
| 102 | struct virtio_driver, driver); |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 103 | u32 device_features; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 104 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 105 | /* We have a driver! */ |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 106 | add_status(dev, VIRTIO_CONFIG_S_DRIVER); |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 107 | |
| 108 | /* Figure out what features the device supports. */ |
| 109 | device_features = dev->config->get_features(dev); |
| 110 | |
| 111 | /* Features supported by both device and driver into dev->features. */ |
| 112 | memset(dev->features, 0, sizeof(dev->features)); |
| 113 | for (i = 0; i < drv->feature_table_size; i++) { |
| 114 | unsigned int f = drv->feature_table[i]; |
| 115 | BUG_ON(f >= 32); |
| 116 | if (device_features & (1 << f)) |
| 117 | set_bit(f, dev->features); |
| 118 | } |
| 119 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 120 | err = drv->probe(dev); |
| 121 | if (err) |
| 122 | add_status(dev, VIRTIO_CONFIG_S_FAILED); |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 123 | else { |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 124 | add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK); |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 125 | /* They should never have set feature bits beyond 32 */ |
| 126 | dev->config->set_features(dev, dev->features[0]); |
| 127 | } |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 131 | static int virtio_dev_remove(struct device *_d) |
| 132 | { |
| 133 | struct virtio_device *dev = container_of(_d,struct virtio_device,dev); |
| 134 | struct virtio_driver *drv = container_of(dev->dev.driver, |
| 135 | struct virtio_driver, driver); |
| 136 | |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 137 | drv->remove(dev); |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 138 | |
| 139 | /* Driver should have reset device. */ |
| 140 | BUG_ON(dev->config->get_status(dev)); |
| 141 | |
| 142 | /* Acknowledge the device's existence again. */ |
| 143 | add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 147 | int register_virtio_driver(struct virtio_driver *driver) |
| 148 | { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 149 | /* Catch this early. */ |
| 150 | BUG_ON(driver->feature_table_size && !driver->feature_table); |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 151 | driver->driver.bus = &virtio_bus; |
| 152 | driver->driver.probe = virtio_dev_probe; |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 153 | driver->driver.remove = virtio_dev_remove; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 154 | return driver_register(&driver->driver); |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(register_virtio_driver); |
| 157 | |
| 158 | void unregister_virtio_driver(struct virtio_driver *driver) |
| 159 | { |
| 160 | driver_unregister(&driver->driver); |
| 161 | } |
| 162 | EXPORT_SYMBOL_GPL(unregister_virtio_driver); |
| 163 | |
| 164 | int register_virtio_device(struct virtio_device *dev) |
| 165 | { |
| 166 | int err; |
| 167 | |
| 168 | dev->dev.bus = &virtio_bus; |
Rusty Russell | 2ad3cfb | 2008-05-30 15:09:41 -0500 | [diff] [blame^] | 169 | sprintf(dev->dev.bus_id, "virtio%u", dev->index); |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 170 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 171 | /* We always start by resetting the device, in case a previous |
| 172 | * driver messed it up. This also tests that code path a little. */ |
| 173 | dev->config->reset(dev); |
| 174 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 175 | /* Acknowledge that we've seen the device. */ |
| 176 | add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); |
| 177 | |
| 178 | /* device_register() causes the bus infrastructure to look for a |
| 179 | * matching driver. */ |
| 180 | err = device_register(&dev->dev); |
| 181 | if (err) |
| 182 | add_status(dev, VIRTIO_CONFIG_S_FAILED); |
| 183 | return err; |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(register_virtio_device); |
| 186 | |
| 187 | void unregister_virtio_device(struct virtio_device *dev) |
| 188 | { |
| 189 | device_unregister(&dev->dev); |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(unregister_virtio_device); |
| 192 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 193 | static int virtio_init(void) |
| 194 | { |
| 195 | if (bus_register(&virtio_bus) != 0) |
| 196 | panic("virtio bus registration failed"); |
| 197 | return 0; |
| 198 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 199 | |
| 200 | static void __exit virtio_exit(void) |
| 201 | { |
| 202 | bus_unregister(&virtio_bus); |
| 203 | } |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 204 | core_initcall(virtio_init); |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 205 | module_exit(virtio_exit); |
| 206 | |
| 207 | MODULE_LICENSE("GPL"); |