[PATCH] Create vio_register_device
[linux-2.6/x86.git] / arch / ppc64 / kernel / vio.c
blob3eab2290b12a2cb683e4eadc20253bde37c84859
1 /*
2 * IBM PowerPC Virtual I/O Infrastructure Support.
4 * Copyright (c) 2003-2005 IBM Corp.
5 * Dave Engebretsen engebret@us.ibm.com
6 * Santiago Leon santil@us.ibm.com
7 * Hollis Blanchard <hollisb@us.ibm.com>
8 * Stephen Rothwell
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/init.h>
17 #include <linux/console.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/dma-mapping.h>
21 #include <asm/iommu.h>
22 #include <asm/dma.h>
23 #include <asm/vio.h>
25 static const struct vio_device_id *vio_match_device(
26 const struct vio_device_id *, const struct vio_dev *);
28 struct vio_dev vio_bus_device = { /* fake "parent" device */
29 .name = vio_bus_device.dev.bus_id,
30 .type = "",
31 .dev.bus_id = "vio",
32 .dev.bus = &vio_bus_type,
35 static int (*is_match)(const struct vio_device_id *id,
36 const struct vio_dev *dev);
37 static void (*unregister_device_callback)(struct vio_dev *dev);
38 static void (*release_device_callback)(struct device *dev);
41 * Convert from struct device to struct vio_dev and pass to driver.
42 * dev->driver has already been set by generic code because vio_bus_match
43 * succeeded.
45 static int vio_bus_probe(struct device *dev)
47 struct vio_dev *viodev = to_vio_dev(dev);
48 struct vio_driver *viodrv = to_vio_driver(dev->driver);
49 const struct vio_device_id *id;
50 int error = -ENODEV;
52 if (!viodrv->probe)
53 return error;
55 id = vio_match_device(viodrv->id_table, viodev);
56 if (id)
57 error = viodrv->probe(viodev, id);
59 return error;
62 /* convert from struct device to struct vio_dev and pass to driver. */
63 static int vio_bus_remove(struct device *dev)
65 struct vio_dev *viodev = to_vio_dev(dev);
66 struct vio_driver *viodrv = to_vio_driver(dev->driver);
68 if (viodrv->remove)
69 return viodrv->remove(viodev);
71 /* driver can't remove */
72 return 1;
75 /**
76 * vio_register_driver: - Register a new vio driver
77 * @drv: The vio_driver structure to be registered.
79 int vio_register_driver(struct vio_driver *viodrv)
81 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
82 viodrv->name);
84 /* fill in 'struct driver' fields */
85 viodrv->driver.name = viodrv->name;
86 viodrv->driver.bus = &vio_bus_type;
87 viodrv->driver.probe = vio_bus_probe;
88 viodrv->driver.remove = vio_bus_remove;
90 return driver_register(&viodrv->driver);
92 EXPORT_SYMBOL(vio_register_driver);
94 /**
95 * vio_unregister_driver - Remove registration of vio driver.
96 * @driver: The vio_driver struct to be removed form registration
98 void vio_unregister_driver(struct vio_driver *viodrv)
100 driver_unregister(&viodrv->driver);
102 EXPORT_SYMBOL(vio_unregister_driver);
105 * vio_match_device: - Tell if a VIO device has a matching
106 * VIO device id structure.
107 * @ids: array of VIO device id structures to search in
108 * @dev: the VIO device structure to match against
110 * Used by a driver to check whether a VIO device present in the
111 * system is in its list of supported devices. Returns the matching
112 * vio_device_id structure or NULL if there is no match.
114 static const struct vio_device_id *vio_match_device(
115 const struct vio_device_id *ids, const struct vio_dev *dev)
117 while (ids->type) {
118 if (is_match(ids, dev))
119 return ids;
120 ids++;
122 return NULL;
126 * vio_bus_init: - Initialize the virtual IO bus
128 int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id,
129 const struct vio_dev *dev),
130 void (*unregister_dev)(struct vio_dev *),
131 void (*release_dev)(struct device *))
133 int err;
135 is_match = match_func;
136 unregister_device_callback = unregister_dev;
137 release_device_callback = release_dev;
139 err = bus_register(&vio_bus_type);
140 if (err) {
141 printk(KERN_ERR "failed to register VIO bus\n");
142 return err;
146 * The fake parent of all vio devices, just to give us
147 * a nice directory
149 err = device_register(&vio_bus_device.dev);
150 if (err) {
151 printk(KERN_WARNING "%s: device_register returned %i\n",
152 __FUNCTION__, err);
153 return err;
156 return 0;
159 /* vio_dev refcount hit 0 */
160 static void __devinit vio_dev_release(struct device *dev)
162 if (release_device_callback)
163 release_device_callback(dev);
164 kfree(to_vio_dev(dev));
167 static ssize_t viodev_show_name(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
172 DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
174 struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
176 /* init generic 'struct device' fields: */
177 viodev->dev.parent = &vio_bus_device.dev;
178 viodev->dev.bus = &vio_bus_type;
179 viodev->dev.release = vio_dev_release;
181 /* register with generic device framework */
182 if (device_register(&viodev->dev)) {
183 printk(KERN_ERR "%s: failed to register device %s\n",
184 __FUNCTION__, viodev->dev.bus_id);
185 return NULL;
187 device_create_file(&viodev->dev, &dev_attr_name);
189 return viodev;
192 void __devinit vio_unregister_device(struct vio_dev *viodev)
194 if (unregister_device_callback)
195 unregister_device_callback(viodev);
196 device_remove_file(&viodev->dev, &dev_attr_name);
197 device_unregister(&viodev->dev);
199 EXPORT_SYMBOL(vio_unregister_device);
201 static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
202 size_t size, enum dma_data_direction direction)
204 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
205 direction);
208 static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
209 size_t size, enum dma_data_direction direction)
211 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
212 direction);
215 static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
216 int nelems, enum dma_data_direction direction)
218 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
219 nelems, direction);
222 static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
223 int nelems, enum dma_data_direction direction)
225 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
228 static void *vio_alloc_coherent(struct device *dev, size_t size,
229 dma_addr_t *dma_handle, unsigned int __nocast flag)
231 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
232 dma_handle, flag);
235 static void vio_free_coherent(struct device *dev, size_t size,
236 void *vaddr, dma_addr_t dma_handle)
238 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
239 dma_handle);
242 static int vio_dma_supported(struct device *dev, u64 mask)
244 return 1;
247 struct dma_mapping_ops vio_dma_ops = {
248 .alloc_coherent = vio_alloc_coherent,
249 .free_coherent = vio_free_coherent,
250 .map_single = vio_map_single,
251 .unmap_single = vio_unmap_single,
252 .map_sg = vio_map_sg,
253 .unmap_sg = vio_unmap_sg,
254 .dma_supported = vio_dma_supported,
257 static int vio_bus_match(struct device *dev, struct device_driver *drv)
259 const struct vio_dev *vio_dev = to_vio_dev(dev);
260 struct vio_driver *vio_drv = to_vio_driver(drv);
261 const struct vio_device_id *ids = vio_drv->id_table;
263 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
266 struct bus_type vio_bus_type = {
267 .name = "vio",
268 .match = vio_bus_match,