1 #include <linux/init.h>
2 #include <linux/bootmem.h>
3 #include <linux/lguest_bus.h>
6 static ssize_t
type_show(struct device
*_dev
,
7 struct device_attribute
*attr
, char *buf
)
9 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
10 return sprintf(buf
, "%hu", lguest_devices
[dev
->index
].type
);
12 static ssize_t
features_show(struct device
*_dev
,
13 struct device_attribute
*attr
, char *buf
)
15 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
16 return sprintf(buf
, "%hx", lguest_devices
[dev
->index
].features
);
18 static ssize_t
pfn_show(struct device
*_dev
,
19 struct device_attribute
*attr
, char *buf
)
21 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
22 return sprintf(buf
, "%u", lguest_devices
[dev
->index
].pfn
);
24 static ssize_t
status_show(struct device
*_dev
,
25 struct device_attribute
*attr
, char *buf
)
27 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
28 return sprintf(buf
, "%hx", lguest_devices
[dev
->index
].status
);
30 static ssize_t
status_store(struct device
*_dev
, struct device_attribute
*attr
,
31 const char *buf
, size_t count
)
33 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
34 if (sscanf(buf
, "%hi", &lguest_devices
[dev
->index
].status
) != 1)
38 static struct device_attribute lguest_dev_attrs
[] = {
42 __ATTR(status
, 0644, status_show
, status_store
),
46 static int lguest_dev_match(struct device
*_dev
, struct device_driver
*_drv
)
48 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
49 struct lguest_driver
*drv
= container_of(_drv
,struct lguest_driver
,drv
);
51 return (drv
->device_type
== lguest_devices
[dev
->index
].type
);
59 static struct lguest_bus lguest_bus
= {
62 .match
= lguest_dev_match
,
63 .dev_attrs
= lguest_dev_attrs
,
71 static int lguest_dev_probe(struct device
*_dev
)
74 struct lguest_device
*dev
= container_of(_dev
,struct lguest_device
,dev
);
75 struct lguest_driver
*drv
= container_of(dev
->dev
.driver
,
76 struct lguest_driver
, drv
);
78 lguest_devices
[dev
->index
].status
|= LGUEST_DEVICE_S_DRIVER
;
79 ret
= drv
->probe(dev
);
81 lguest_devices
[dev
->index
].status
|= LGUEST_DEVICE_S_DRIVER_OK
;
85 int register_lguest_driver(struct lguest_driver
*drv
)
90 drv
->drv
.bus
= &lguest_bus
.bus
;
91 drv
->drv
.name
= drv
->name
;
92 drv
->drv
.owner
= drv
->owner
;
93 drv
->drv
.probe
= lguest_dev_probe
;
95 return driver_register(&drv
->drv
);
97 EXPORT_SYMBOL_GPL(register_lguest_driver
);
99 static void add_lguest_device(unsigned int index
)
101 struct lguest_device
*new;
103 lguest_devices
[index
].status
|= LGUEST_DEVICE_S_ACKNOWLEDGE
;
104 new = kmalloc(sizeof(struct lguest_device
), GFP_KERNEL
);
106 printk(KERN_EMERG
"Cannot allocate lguest device %u\n", index
);
107 lguest_devices
[index
].status
|= LGUEST_DEVICE_S_FAILED
;
113 memset(&new->dev
, 0, sizeof(new->dev
));
114 new->dev
.parent
= &lguest_bus
.dev
;
115 new->dev
.bus
= &lguest_bus
.bus
;
116 sprintf(new->dev
.bus_id
, "%u", index
);
117 if (device_register(&new->dev
) != 0) {
118 printk(KERN_EMERG
"Cannot register lguest device %u\n", index
);
119 lguest_devices
[index
].status
|= LGUEST_DEVICE_S_FAILED
;
124 static void scan_devices(void)
128 for (i
= 0; i
< LGUEST_MAX_DEVICES
; i
++)
129 if (lguest_devices
[i
].type
)
130 add_lguest_device(i
);
133 static int __init
lguest_bus_init(void)
135 if (strcmp(paravirt_ops
.name
, "lguest") != 0)
138 /* Devices are in page above top of "normal" mem. */
139 lguest_devices
= lguest_map(max_pfn
<<PAGE_SHIFT
, 1);
141 if (bus_register(&lguest_bus
.bus
) != 0
142 || device_register(&lguest_bus
.dev
) != 0)
143 panic("lguest bus registration failed");
148 postcore_initcall(lguest_bus_init
);