iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / virtio / virtio.c
blobb535483bc556f5f153eb434583c8dc6593377164
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 ssize_t modalias_show(struct device *_d,
24 struct device_attribute *attr, char *buf)
26 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);
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
39 static inline int virtio_id_match(const struct virtio_device *dev,
40 const struct virtio_device_id *id)
42 if (id->device != dev->id.device)
43 return 0;
45 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor != dev->id.vendor;
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)
52 unsigned int i;
53 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
54 const struct virtio_device_id *ids;
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;
63 static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
65 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
67 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
68 dev->id.device, dev->id.vendor);
71 static struct bus_type virtio_bus = {
72 .name = "virtio",
73 .match = virtio_dev_match,
74 .dev_attrs = virtio_dev_attrs,
75 .uevent = virtio_uevent,
78 static void add_status(struct virtio_device *dev, unsigned status)
80 dev->config->set_status(dev, dev->config->get_status(dev) | status);
83 static int virtio_dev_probe(struct device *_d)
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);
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;
99 static int virtio_dev_remove(struct device *_d)
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);
105 drv->remove(dev);
107 /* Driver should have reset device. */
108 BUG_ON(dev->config->get_status(dev));
110 /* Acknowledge the device's existence again. */
111 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
112 return 0;
115 int register_virtio_driver(struct virtio_driver *driver)
117 driver->driver.bus = &virtio_bus;
118 driver->driver.probe = virtio_dev_probe;
119 driver->driver.remove = virtio_dev_remove;
120 return driver_register(&driver->driver);
122 EXPORT_SYMBOL_GPL(register_virtio_driver);
124 void unregister_virtio_driver(struct virtio_driver *driver)
126 driver_unregister(&driver->driver);
128 EXPORT_SYMBOL_GPL(unregister_virtio_driver);
130 int register_virtio_device(struct virtio_device *dev)
132 int err;
134 dev->dev.bus = &virtio_bus;
135 sprintf(dev->dev.bus_id, "%u", dev->index);
137 /* We always start by resetting the device, in case a previous
138 * driver messed it up. This also tests that code path a little. */
139 dev->config->reset(dev);
141 /* Acknowledge that we've seen the device. */
142 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
144 /* device_register() causes the bus infrastructure to look for a
145 * matching driver. */
146 err = device_register(&dev->dev);
147 if (err)
148 add_status(dev, VIRTIO_CONFIG_S_FAILED);
149 return err;
151 EXPORT_SYMBOL_GPL(register_virtio_device);
153 void unregister_virtio_device(struct virtio_device *dev)
155 device_unregister(&dev->dev);
157 EXPORT_SYMBOL_GPL(unregister_virtio_device);
159 static int virtio_init(void)
161 if (bus_register(&virtio_bus) != 0)
162 panic("virtio bus registration failed");
163 return 0;
166 static void __exit virtio_exit(void)
168 bus_unregister(&virtio_bus);
170 core_initcall(virtio_init);
171 module_exit(virtio_exit);
173 MODULE_LICENSE("GPL");