Update OF platform & macio driver cores to adapt to device

model changes. Fix refcounting
parent 394edd85
......@@ -15,8 +15,8 @@
* Used by a driver to check whether an of_device present in the
* system is in its list of supported devices.
*/
const struct of_match *
of_match_device(const struct of_match *matches, const struct of_device *dev)
const struct of_match * of_match_device(const struct of_match *matches,
const struct of_device *dev)
{
if (!dev->node)
return NULL;
......@@ -38,8 +38,7 @@ of_match_device(const struct of_match *matches, const struct of_device *dev)
return NULL;
}
static int
of_platform_bus_match(struct device *dev, struct device_driver *drv)
static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
{
struct of_device * of_dev = to_of_device(dev);
struct of_platform_driver * of_drv = to_of_platform_driver(drv);
......@@ -51,21 +50,27 @@ of_platform_bus_match(struct device *dev, struct device_driver *drv)
return of_match_device(matches, of_dev) != NULL;
}
struct bus_type of_platform_bus_type = {
name: "of_platform",
match: of_platform_bus_match,
};
struct of_device *of_dev_get(struct of_device *dev)
{
struct device *tmp;
if (!dev)
return NULL;
tmp = get_device(&dev->dev);
if (tmp)
return to_of_device(tmp);
else
return NULL;
}
static int __init
of_bus_driver_init(void)
void of_dev_put(struct of_device *dev)
{
return bus_register(&of_platform_bus_type);
if (dev)
put_device(&dev->dev);
}
postcore_initcall(of_bus_driver_init);
static int
of_device_probe(struct device *dev)
static int of_device_probe(struct device *dev)
{
int error = -ENODEV;
struct of_platform_driver *drv;
......@@ -78,22 +83,18 @@ of_device_probe(struct device *dev)
if (!drv->probe)
return error;
/* if (!try_module_get(driver->owner)) {
printk(KERN_ERR "Can't get a module reference for %s\n", driver->name);
return error;
}
*/
of_dev_get(of_dev);
match = of_match_device(drv->match_table, of_dev);
if (match)
error = drv->probe(of_dev, match);
/*
module_put(driver->owner);
*/
if (error)
of_dev_put(of_dev);
return error;
}
static int
of_device_remove(struct device *dev)
static int of_device_remove(struct device *dev)
{
struct of_device * of_dev = to_of_device(dev);
struct of_platform_driver * drv = to_of_platform_driver(of_dev->dev.driver);
......@@ -103,32 +104,43 @@ of_device_remove(struct device *dev)
return 0;
}
static int
of_device_suspend(struct device *dev, u32 state, u32 level)
static int of_device_suspend(struct device *dev, u32 state)
{
struct of_device * of_dev = to_of_device(dev);
struct of_platform_driver * drv = to_of_platform_driver(of_dev->dev.driver);
int error = 0;
if (drv && drv->suspend)
error = drv->suspend(of_dev, state, level);
error = drv->suspend(of_dev, state);
return error;
}
static int
of_device_resume(struct device * dev, u32 level)
static int of_device_resume(struct device * dev)
{
struct of_device * of_dev = to_of_device(dev);
struct of_platform_driver * drv = to_of_platform_driver(of_dev->dev.driver);
int error = 0;
if (drv && drv->resume)
error = drv->resume(of_dev, level);
error = drv->resume(of_dev);
return error;
}
int
of_register_driver(struct of_platform_driver *drv)
struct bus_type of_platform_bus_type = {
name: "of_platform",
match: of_platform_bus_match,
suspend: of_device_suspend,
resume: of_device_resume,
};
static int __init of_bus_driver_init(void)
{
return bus_register(&of_platform_bus_type);
}
postcore_initcall(of_bus_driver_init);
int of_register_driver(struct of_platform_driver *drv)
{
int count = 0;
......@@ -136,8 +148,6 @@ of_register_driver(struct of_platform_driver *drv)
drv->driver.name = drv->name;
drv->driver.bus = &of_platform_bus_type;
drv->driver.probe = of_device_probe;
drv->driver.resume = of_device_resume;
drv->driver.suspend = of_device_suspend;
drv->driver.remove = of_device_remove;
/* register with core */
......@@ -145,15 +155,13 @@ of_register_driver(struct of_platform_driver *drv)
return count ? count : 1;
}
void
of_unregister_driver(struct of_platform_driver *drv)
void of_unregister_driver(struct of_platform_driver *drv)
{
driver_unregister(&drv->driver);
}
static ssize_t
dev_show_devspec(struct device *dev, char *buf)
static ssize_t dev_show_devspec(struct device *dev, char *buf)
{
struct of_device *ofdev;
......@@ -163,8 +171,22 @@ dev_show_devspec(struct device *dev, char *buf)
static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
int
of_device_register(struct of_device *ofdev)
/**
* of_release_dev - free an of device structure when all users of it are finished.
* @dev: device that's been disconnected
*
* Will be called only by the device core when all users of this of device are
* done.
*/
void of_release_dev(struct device *dev)
{
struct of_device *ofdev;
ofdev = to_of_device(dev);
kfree(ofdev);
}
int of_device_register(struct of_device *ofdev)
{
int rc;
struct of_device **odprop;
......@@ -197,21 +219,20 @@ of_device_register(struct of_device *ofdev)
return 0;
}
void
of_device_unregister(struct of_device *ofdev)
void of_device_unregister(struct of_device *ofdev)
{
struct of_device **odprop;
device_remove_file(&ofdev->dev, &dev_attr_devspec);
device_unregister(&ofdev->dev);
odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
if (odprop)
*odprop = NULL;
device_unregister(&ofdev->dev);
}
struct of_device*
of_platform_device_create(struct device_node *np, const char *bus_id)
struct of_device* of_platform_device_create(struct device_node *np, const char *bus_id)
{
struct of_device *dev;
u32 *reg;
......@@ -226,6 +247,7 @@ of_platform_device_create(struct device_node *np, const char *bus_id)
dev->dev.dma_mask = &dev->dma_mask;
dev->dev.parent = NULL;
dev->dev.bus = &of_platform_bus_type;
dev->dev.release = of_release_dev;
reg = (u32 *)get_property(np, "reg", NULL);
strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
......@@ -244,4 +266,7 @@ EXPORT_SYMBOL(of_register_driver);
EXPORT_SYMBOL(of_unregister_driver);
EXPORT_SYMBOL(of_device_register);
EXPORT_SYMBOL(of_device_unregister);
EXPORT_SYMBOL(of_dev_get);
EXPORT_SYMBOL(of_dev_put);
EXPORT_SYMBOL(of_platform_device_create);
EXPORT_SYMBOL(of_release_dev);
......@@ -23,6 +23,8 @@
#include <asm/prom.h>
#include <asm/pci-bridge.h>
static struct macio_chip *macio_on_hold;
static int
macio_bus_match(struct device *dev, struct device_driver *drv)
{
......@@ -36,21 +38,27 @@ macio_bus_match(struct device *dev, struct device_driver *drv)
return of_match_device(matches, &macio_dev->ofdev) != NULL;
}
struct bus_type macio_bus_type = {
name: "macio",
match: macio_bus_match,
};
struct macio_dev *macio_dev_get(struct macio_dev *dev)
{
struct device *tmp;
static int __init
macio_bus_driver_init(void)
if (!dev)
return NULL;
tmp = get_device(&dev->ofdev.dev);
if (tmp)
return to_macio_device(tmp);
else
return NULL;
}
void macio_dev_put(struct macio_dev *dev)
{
return bus_register(&macio_bus_type);
if (dev)
put_device(&dev->ofdev.dev);
}
postcore_initcall(macio_bus_driver_init);
static int
macio_device_probe(struct device *dev)
static int macio_device_probe(struct device *dev)
{
int error = -ENODEV;
struct macio_driver *drv;
......@@ -63,55 +71,89 @@ macio_device_probe(struct device *dev)
if (!drv->probe)
return error;
/* if (!try_module_get(driver->owner)) {
printk(KERN_ERR "Can't get a module reference for %s\n", driver->name);
return error;
}
*/
macio_dev_get(macio_dev);
match = of_match_device(drv->match_table, &macio_dev->ofdev);
if (match)
error = drv->probe(macio_dev, match);
/*
module_put(driver->owner);
*/
if (error)
macio_dev_put(macio_dev);
return error;
}
static int
macio_device_remove(struct device *dev)
static int macio_device_remove(struct device *dev)
{
struct macio_dev * macio_dev = to_macio_device(dev);
struct macio_driver * drv = to_macio_driver(macio_dev->ofdev.dev.driver);
if (drv && drv->remove)
drv->remove(macio_dev);
macio_dev_put(macio_dev);
return 0;
}
static int
macio_device_suspend(struct device *dev, u32 state, u32 level)
static int macio_device_suspend(struct device *dev, u32 state)
{
struct macio_dev * macio_dev = to_macio_device(dev);
struct macio_driver * drv = to_macio_driver(macio_dev->ofdev.dev.driver);
struct macio_driver * drv;
int error = 0;
if (drv && drv->suspend)
error = drv->suspend(macio_dev, state, level);
if (macio_dev->ofdev.dev.driver == NULL)
return 0;
drv = to_macio_driver(macio_dev->ofdev.dev.driver);
if (drv->suspend)
error = drv->suspend(macio_dev, state);
return error;
}
static int
macio_device_resume(struct device * dev, u32 level)
static int macio_device_resume(struct device * dev)
{
struct macio_dev * macio_dev = to_macio_device(dev);
struct macio_driver * drv = to_macio_driver(macio_dev->ofdev.dev.driver);
struct macio_driver * drv;
int error = 0;
if (drv && drv->resume)
error = drv->resume(macio_dev, level);
if (macio_dev->ofdev.dev.driver == NULL)
return 0;
drv = to_macio_driver(macio_dev->ofdev.dev.driver);
if (drv->resume)
error = drv->resume(macio_dev);
return error;
}
struct bus_type macio_bus_type = {
name: "macio",
match: macio_bus_match,
suspend: macio_device_suspend,
resume: macio_device_resume,
};
static int __init
macio_bus_driver_init(void)
{
return bus_register(&macio_bus_type);
}
postcore_initcall(macio_bus_driver_init);
/**
* macio_release_dev - free a macio device structure when all users of it are finished.
* @dev: device that's been disconnected
*
* Will be called only by the device core when all users of this macio device are
* done. This currently means never as we don't hot remove any macio device yet,
* though that will happen with mediabay based devices in a later implementation.
*/
static void macio_release_dev(struct device *dev)
{
struct macio_dev *mdev;
mdev = to_macio_device(dev);
kfree(mdev);
}
/**
* macio_add_one_device - Add one device from OF node to the device tree
* @chip: pointer to the macio_chip holding the device
......@@ -121,13 +163,15 @@ macio_device_resume(struct device * dev, u32 level)
* When media-bay is changed to hotswap drivers, this function will
* be exposed to the bay driver some way...
*/
static struct macio_dev *
macio_add_one_device(struct macio_chip *chip, struct device *parent,
static struct macio_dev * macio_add_one_device(struct macio_chip *chip, struct device *parent,
struct device_node *np, struct macio_dev *in_bay)
{
struct macio_dev *dev;
u32 *reg;
if (np == NULL)
return NULL;
dev = kmalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return NULL;
......@@ -140,6 +184,7 @@ macio_add_one_device(struct macio_chip *chip, struct device *parent,
dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask;
dev->ofdev.dev.parent = parent;
dev->ofdev.dev.bus = &macio_bus_type;
dev->ofdev.dev.release = macio_release_dev;
/* MacIO itself has a different reg, we use it's PCI base */
if (np == chip->of_node) {
......@@ -164,8 +209,7 @@ macio_add_one_device(struct macio_chip *chip, struct device *parent,
return dev;
}
static int
macio_skip_device(struct device_node *np)
static int macio_skip_device(struct device_node *np)
{
if (strncmp(np->name, "battery", 7) == 0)
return 1;
......@@ -185,10 +229,9 @@ macio_skip_device(struct device_node *np)
* For now, childs of media-bay are added now as well. This will
* change rsn though.
*/
static void
macio_pci_add_devices(struct macio_chip *chip)
static void macio_pci_add_devices(struct macio_chip *chip)
{
struct device_node *np;
struct device_node *np, *pnode;
struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
struct device *parent = NULL;
......@@ -197,15 +240,22 @@ macio_pci_add_devices(struct macio_chip *chip)
if (chip->lbus.pdev)
parent = &chip->lbus.pdev->dev;
#endif
rdev = macio_add_one_device(chip, parent, chip->of_node, NULL);
pnode = of_node_get(chip->of_node);
if (pnode == NULL)
return;
rdev = macio_add_one_device(chip, parent, pnode, NULL);
if (rdev == NULL)
return;
/* First scan 1st level */
for (np = chip->of_node->child; np != NULL; np = np->sibling) {
for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
if (!macio_skip_device(np)) {
of_node_get(np);
mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL);
if (strncmp(np->name, "media-bay", 9) == 0)
if (mdev == NULL)
of_node_put(np);
else if (strncmp(np->name, "media-bay", 9) == 0)
mbdev = mdev;
else if (strncmp(np->name, "escc", 4) == 0)
sdev = mdev;
......@@ -213,16 +263,20 @@ macio_pci_add_devices(struct macio_chip *chip)
}
/* Add media bay devices if any */
if (mbdev) {
for (np = mbdev->ofdev.node->child; np != NULL; np = np->sibling)
if (!macio_skip_device(np))
macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev);
if (mbdev)
for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
if (!macio_skip_device(np)) {
of_node_get(np);
if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev) == NULL)
of_node_put(np);
}
/* Add serial ports if any */
if (sdev) {
for (np = sdev->ofdev.node->child; np != NULL; np = np->sibling)
if (!macio_skip_device(np))
macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL);
if (sdev)
for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
if (!macio_skip_device(np)) {
of_node_get(np);
if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL) == NULL)
of_node_put(np);
}
}
......@@ -231,8 +285,7 @@ macio_pci_add_devices(struct macio_chip *chip)
* macio_register_driver - Registers a new MacIO device driver
* @drv: pointer to the driver definition structure
*/
int
macio_register_driver(struct macio_driver *drv)
int macio_register_driver(struct macio_driver *drv)
{
int count = 0;
......@@ -240,8 +293,6 @@ macio_register_driver(struct macio_driver *drv)
drv->driver.name = drv->name;
drv->driver.bus = &macio_bus_type;
drv->driver.probe = macio_device_probe;
drv->driver.resume = macio_device_resume;
drv->driver.suspend = macio_device_suspend;
drv->driver.remove = macio_device_remove;
/* register with core */
......@@ -253,16 +304,14 @@ macio_register_driver(struct macio_driver *drv)
* macio_unregister_driver - Unregisters a new MacIO device driver
* @drv: pointer to the driver definition structure
*/
void
macio_unregister_driver(struct macio_driver *drv)
void macio_unregister_driver(struct macio_driver *drv)
{
driver_unregister(&drv->driver);
}
#ifdef CONFIG_PCI
static int __devinit
macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
struct device_node* np;
struct macio_chip* chip;
......@@ -270,19 +319,27 @@ macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
if (ent->vendor != PCI_VENDOR_ID_APPLE)
return -ENODEV;
/* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
* to new OF APIs and returns a node with refcount incremented. This isn't
* the case today, but on the other hand ppc32 doesn't do refcounting. This
* will have to be fixed when going to ppc64. --BenH.
*/
np = pci_device_to_OF_node(pdev);
if (np == NULL)
return -ENODEV;
/* We also assume that pmac_feature will have done a get() on nodes stored
* in the macio chips array
*/
chip = macio_find(np, macio_unknown);
of_node_put(np);
if (chip == NULL)
return -ENODEV;
/* XXX Need locking */
/* XXX Need locking ??? */
if (chip->lbus.pdev == NULL) {
chip->lbus.pdev = pdev;
chip->lbus.chip = chip;
// INIT_LIST_HEAD(&chip->lbus.devices);
pci_set_drvdata(pdev, &chip->lbus);
pci_set_master(pdev);
}
......@@ -290,13 +347,28 @@ macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
chip->name);
/*
* HACK ALERT: The WallStreet PowerBook and some OHare based machines
* have 2 macio ASICs. I must probe the "main" one first or IDE ordering
* will be incorrect. So I put on "hold" the second one since it seem to
* appear first on PCI
*/
if (chip->type == macio_gatwick || chip->type == macio_ohareII)
if (macio_chips[0].lbus.pdev == NULL) {
macio_on_hold = chip;
return 0;
}
macio_pci_add_devices(chip);
if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
macio_pci_add_devices(macio_on_hold);
macio_on_hold = NULL;
}
return 0;
}
static void __devexit
macio_pci_remove(struct pci_dev* pdev)
static void __devexit macio_pci_remove(struct pci_dev* pdev)
{
panic("removing of macio-asic not supported !\n");
}
......@@ -326,8 +398,7 @@ static struct pci_driver macio_pci_driver = {
#endif /* CONFIG_PCI */
static int __init
macio_module_init (void)
static int __init macio_module_init (void)
{
#ifdef CONFIG_PCI
int rc;
......@@ -339,17 +410,9 @@ macio_module_init (void)
return 0;
}
/*
static void __exit
macio_module_cleanup (void)
{
#ifdef CONFIG_PCI
pci_unregister_driver(&macio_pci_driver);
#endif
}
module_exit(macio_module_cleanup);
*/
module_init(macio_module_init);
EXPORT_SYMBOL(macio_register_driver);
EXPORT_SYMBOL(macio_unregister_driver);
EXPORT_SYMBOL(macio_dev_get);
EXPORT_SYMBOL(macio_dev_put);
......@@ -42,6 +42,9 @@ struct macio_dev
#define to_macio_device(d) container_of(d, struct macio_dev, ofdev.dev)
#define of_to_macio_device(d) container_of(d, struct macio_dev, ofdev)
extern struct macio_dev *macio_dev_get(struct macio_dev *dev);
extern void macio_dev_put(struct macio_dev *dev);
/*
* A driver for a mac-io chip based device
*/
......@@ -54,8 +57,8 @@ struct macio_driver
int (*probe)(struct macio_dev* dev, const struct of_match *match);
int (*remove)(struct macio_dev* dev);
int (*suspend)(struct macio_dev* dev, u32 state, u32 level);
int (*resume)(struct macio_dev* dev, u32 level);
int (*suspend)(struct macio_dev* dev, u32 state);
int (*resume)(struct macio_dev* dev);
int (*shutdown)(struct macio_dev* dev);
struct device_driver driver;
......
......@@ -39,6 +39,9 @@ struct of_match
extern const struct of_match *of_match_device(
const struct of_match *matches, const struct of_device *dev);
extern struct of_device *of_dev_get(struct of_device *dev);
extern void of_dev_put(struct of_device *dev);
/*
* An of_platform_driver driver is attached to a basic of_device on
* the "platform bus" (of_platform_bus_type)
......@@ -52,8 +55,8 @@ struct of_platform_driver
int (*probe)(struct of_device* dev, const struct of_match *match);
int (*remove)(struct of_device* dev);
int (*suspend)(struct of_device* dev, u32 state, u32 level);
int (*resume)(struct of_device* dev, u32 level);
int (*suspend)(struct of_device* dev, u32 state);
int (*resume)(struct of_device* dev);
int (*shutdown)(struct of_device* dev);
struct device_driver driver;
......@@ -65,6 +68,7 @@ extern void of_unregister_driver(struct of_platform_driver *drv);
extern int of_device_register(struct of_device *ofdev);
extern void of_device_unregister(struct of_device *ofdev);
extern struct of_device *of_platform_device_create(struct device_node *np, const char *bus_id);
extern void of_release_dev(struct device *dev);
#endif /* __OF_DEVICE_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment