9
0
Fork 0
barebox/drivers/base/driver.c

426 lines
8.2 KiB
C
Raw Normal View History

2007-07-05 16:02:19 +00:00
/*
* driver.c - barebox driver model
2007-07-05 16:02:19 +00:00
*
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
2007-11-08 11:01:52 +00:00
/**
* @file
* @brief barebox's driver model, and devinfo command
2007-11-08 11:01:52 +00:00
*/
2007-07-05 16:02:12 +00:00
#include <common.h>
#include <command.h>
#include <driver.h>
#include <malloc.h>
#include <console.h>
2007-07-05 16:02:12 +00:00
#include <linux/ctype.h>
#include <errno.h>
#include <fs.h>
#include <of.h>
#include <linux/list.h>
#include <complete.h>
#include <pinctrl.h>
2007-07-05 16:02:12 +00:00
LIST_HEAD(device_list);
EXPORT_SYMBOL(device_list);
2007-07-05 16:02:12 +00:00
LIST_HEAD(driver_list);
EXPORT_SYMBOL(driver_list);
2007-07-05 16:02:12 +00:00
static LIST_HEAD(active);
struct device_d *get_device_by_name(const char *name)
{
struct device_d *dev;
for_each_device(dev) {
if(!strcmp(dev_name(dev), name))
return dev;
}
return NULL;
}
static struct device_d *get_device_by_name_id(const char *name, int id)
{
struct device_d *dev;
for_each_device(dev) {
if(!strcmp(dev->name, name) && id == dev->id)
return dev;
}
return NULL;
}
int get_free_deviceid(const char *name_template)
2007-07-05 16:02:12 +00:00
{
int i = 0;
while (1) {
if (!get_device_by_name_id(name_template, i))
return i;
2007-07-05 16:02:12 +00:00
i++;
};
}
int device_probe(struct device_d *dev)
{
int ret;
pinctrl_select_state_default(dev);
ret = dev->bus->probe(dev);
if (ret) {
dev_err(dev, "probe failed: %s\n", strerror(-ret));
return ret;
}
list_add(&dev->active, &active);
return 0;
}
int device_detect(struct device_d *dev)
{
if (!dev->detect)
return -ENOSYS;
return dev->detect(dev);
}
int device_detect_by_name(const char *devname)
{
struct device_d *dev = get_device_by_name(devname);
if (!dev)
return -ENODEV;
return device_detect(dev);
}
2007-07-05 16:02:12 +00:00
static int match(struct driver_d *drv, struct device_d *dev)
{
int ret;
if (dev->driver)
return -1;
2007-10-11 18:56:18 +00:00
dev->driver = drv;
2007-07-05 16:02:12 +00:00
if (dev->bus->match(dev, drv))
goto err_out;
ret = device_probe(dev);
if (ret)
goto err_out;
2007-10-11 18:56:18 +00:00
return 0;
err_out:
dev->driver = NULL;
return -1;
2007-07-05 16:02:12 +00:00
}
int register_device(struct device_d *new_device)
{
struct driver_d *drv;
if (new_device->id == DEVICE_ID_DYNAMIC) {
new_device->id = get_free_deviceid(new_device->name);
} else {
if (get_device_by_name_id(new_device->name, new_device->id)) {
eprintf("register_device: already registered %s\n",
dev_name(new_device));
return -EINVAL;
}
}
debug ("register_device: %s\n", dev_name(new_device));
2007-07-05 16:02:12 +00:00
list_add_tail(&new_device->list, &device_list);
INIT_LIST_HEAD(&new_device->children);
INIT_LIST_HEAD(&new_device->cdevs);
INIT_LIST_HEAD(&new_device->parameters);
INIT_LIST_HEAD(&new_device->active);
INIT_LIST_HEAD(&new_device->bus_list);
2007-07-05 16:02:12 +00:00
drivers/base: fix corrupt device tree dev_add_child is a very unsafe function. If called multiple times it allows setting the same device to different parents thus corrupting the siblings list. This happens regularly since: | commit c2e568d19c5c34a05a1002d25280bf113b72b752 | Author: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | Date: Sat Nov 3 16:11:05 2012 +0100 | | bus: add bus device | | automatically add it as parent of any bus device if none already specified | | we have now a nice output per bus If for example a FATfs is mounted this nice output per bus often ends with: > `---- fat0 > `---- 0 > `---- 0x86f0000087020031-0x86f000410df27124: /dev/<NULL> > `---- sram00 > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > unable to handle NULL pointer dereference at address 0x0000000c > pc : [<87f08a20>] lr : [<87f08a04>] > sp : 86eff8c0 ip : 87f3fbde fp : ffffffff > r10: ffffffff r9 : 00000000 r8 : 00000003 > r7 : 86f075b8 r6 : 00000002 r5 : ffffffec r4 : 86f07544 > r3 : 00000000 r2 : 43f900b4 r1 : 00000020 r0 : 00000005 > Flags: Nzcv IRQs off FIQs off Mode SVC_32 > [<87f08a20>] (do_devinfo_subtree+0x90/0x130) from [<87f08a90>] (do_devinfo_subtree+0x100/0x130) > > [<87f3e070>] (unwind_backtrace+0x0/0x90) from [<87f28514>] (panic+0x28/0x3c) > [<87f28514>] (panic+0x28/0x3c) from [<87f3e4b8>] (do_exception+0x10/0x14) > [<87f3e4b8>] (do_exception+0x10/0x14) from [<87f3e544>] (do_data_abort+0x2c/0x38) > [<87f3e544>] (do_data_abort+0x2c/0x38) from [<87f3e268>] (data_abort+0x48/0x60) This patch fixes this by adding a device to its parents children list in register_device so that dev_add_child is no longer needed. This function is removed from the tree. Now callers of register_device have to clearly set the parent *before* registering a device. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Reported-by: Jan Lübbe <jlu@pengutronix.de>
2012-12-12 13:55:40 +00:00
if (new_device->bus) {
if (!new_device->parent)
new_device->parent = new_device->bus->dev;
drivers/base: fix corrupt device tree dev_add_child is a very unsafe function. If called multiple times it allows setting the same device to different parents thus corrupting the siblings list. This happens regularly since: | commit c2e568d19c5c34a05a1002d25280bf113b72b752 | Author: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | Date: Sat Nov 3 16:11:05 2012 +0100 | | bus: add bus device | | automatically add it as parent of any bus device if none already specified | | we have now a nice output per bus If for example a FATfs is mounted this nice output per bus often ends with: > `---- fat0 > `---- 0 > `---- 0x86f0000087020031-0x86f000410df27124: /dev/<NULL> > `---- sram00 > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > unable to handle NULL pointer dereference at address 0x0000000c > pc : [<87f08a20>] lr : [<87f08a04>] > sp : 86eff8c0 ip : 87f3fbde fp : ffffffff > r10: ffffffff r9 : 00000000 r8 : 00000003 > r7 : 86f075b8 r6 : 00000002 r5 : ffffffec r4 : 86f07544 > r3 : 00000000 r2 : 43f900b4 r1 : 00000020 r0 : 00000005 > Flags: Nzcv IRQs off FIQs off Mode SVC_32 > [<87f08a20>] (do_devinfo_subtree+0x90/0x130) from [<87f08a90>] (do_devinfo_subtree+0x100/0x130) > > [<87f3e070>] (unwind_backtrace+0x0/0x90) from [<87f28514>] (panic+0x28/0x3c) > [<87f28514>] (panic+0x28/0x3c) from [<87f3e4b8>] (do_exception+0x10/0x14) > [<87f3e4b8>] (do_exception+0x10/0x14) from [<87f3e544>] (do_data_abort+0x2c/0x38) > [<87f3e544>] (do_data_abort+0x2c/0x38) from [<87f3e268>] (data_abort+0x48/0x60) This patch fixes this by adding a device to its parents children list in register_device so that dev_add_child is no longer needed. This function is removed from the tree. Now callers of register_device have to clearly set the parent *before* registering a device. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Reported-by: Jan Lübbe <jlu@pengutronix.de>
2012-12-12 13:55:40 +00:00
list_add_tail(&new_device->bus_list, &new_device->bus->device_list);
drivers/base: fix corrupt device tree dev_add_child is a very unsafe function. If called multiple times it allows setting the same device to different parents thus corrupting the siblings list. This happens regularly since: | commit c2e568d19c5c34a05a1002d25280bf113b72b752 | Author: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | Date: Sat Nov 3 16:11:05 2012 +0100 | | bus: add bus device | | automatically add it as parent of any bus device if none already specified | | we have now a nice output per bus If for example a FATfs is mounted this nice output per bus often ends with: > `---- fat0 > `---- 0 > `---- 0x86f0000087020031-0x86f000410df27124: /dev/<NULL> > `---- sram00 > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > unable to handle NULL pointer dereference at address 0x0000000c > pc : [<87f08a20>] lr : [<87f08a04>] > sp : 86eff8c0 ip : 87f3fbde fp : ffffffff > r10: ffffffff r9 : 00000000 r8 : 00000003 > r7 : 86f075b8 r6 : 00000002 r5 : ffffffec r4 : 86f07544 > r3 : 00000000 r2 : 43f900b4 r1 : 00000020 r0 : 00000005 > Flags: Nzcv IRQs off FIQs off Mode SVC_32 > [<87f08a20>] (do_devinfo_subtree+0x90/0x130) from [<87f08a90>] (do_devinfo_subtree+0x100/0x130) > > [<87f3e070>] (unwind_backtrace+0x0/0x90) from [<87f28514>] (panic+0x28/0x3c) > [<87f28514>] (panic+0x28/0x3c) from [<87f3e4b8>] (do_exception+0x10/0x14) > [<87f3e4b8>] (do_exception+0x10/0x14) from [<87f3e544>] (do_data_abort+0x2c/0x38) > [<87f3e544>] (do_data_abort+0x2c/0x38) from [<87f3e268>] (data_abort+0x48/0x60) This patch fixes this by adding a device to its parents children list in register_device so that dev_add_child is no longer needed. This function is removed from the tree. Now callers of register_device have to clearly set the parent *before* registering a device. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Reported-by: Jan Lübbe <jlu@pengutronix.de>
2012-12-12 13:55:40 +00:00
bus_for_each_driver(new_device->bus, drv) {
if (!match(drv, new_device))
break;
}
2007-10-11 18:56:18 +00:00
}
2007-07-05 16:02:12 +00:00
drivers/base: fix corrupt device tree dev_add_child is a very unsafe function. If called multiple times it allows setting the same device to different parents thus corrupting the siblings list. This happens regularly since: | commit c2e568d19c5c34a05a1002d25280bf113b72b752 | Author: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | Date: Sat Nov 3 16:11:05 2012 +0100 | | bus: add bus device | | automatically add it as parent of any bus device if none already specified | | we have now a nice output per bus If for example a FATfs is mounted this nice output per bus often ends with: > `---- fat0 > `---- 0 > `---- 0x86f0000087020031-0x86f000410df27124: /dev/<NULL> > `---- sram00 > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > unable to handle NULL pointer dereference at address 0x0000000c > pc : [<87f08a20>] lr : [<87f08a04>] > sp : 86eff8c0 ip : 87f3fbde fp : ffffffff > r10: ffffffff r9 : 00000000 r8 : 00000003 > r7 : 86f075b8 r6 : 00000002 r5 : ffffffec r4 : 86f07544 > r3 : 00000000 r2 : 43f900b4 r1 : 00000020 r0 : 00000005 > Flags: Nzcv IRQs off FIQs off Mode SVC_32 > [<87f08a20>] (do_devinfo_subtree+0x90/0x130) from [<87f08a90>] (do_devinfo_subtree+0x100/0x130) > > [<87f3e070>] (unwind_backtrace+0x0/0x90) from [<87f28514>] (panic+0x28/0x3c) > [<87f28514>] (panic+0x28/0x3c) from [<87f3e4b8>] (do_exception+0x10/0x14) > [<87f3e4b8>] (do_exception+0x10/0x14) from [<87f3e544>] (do_data_abort+0x2c/0x38) > [<87f3e544>] (do_data_abort+0x2c/0x38) from [<87f3e268>] (data_abort+0x48/0x60) This patch fixes this by adding a device to its parents children list in register_device so that dev_add_child is no longer needed. This function is removed from the tree. Now callers of register_device have to clearly set the parent *before* registering a device. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Reported-by: Jan Lübbe <jlu@pengutronix.de>
2012-12-12 13:55:40 +00:00
if (new_device->parent)
list_add_tail(&new_device->sibling, &new_device->parent->children);
2007-07-05 16:02:12 +00:00
return 0;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(register_device);
2007-07-05 16:02:12 +00:00
int unregister_device(struct device_d *old_dev)
2007-07-05 16:02:12 +00:00
{
struct cdev *cdev, *ct;
struct device_d *child, *dt;
2007-07-05 16:02:12 +00:00
dev_dbg(old_dev, "unregister\n");
dev_remove_parameters(old_dev);
if (old_dev->driver)
old_dev->bus->remove(old_dev);
2007-07-05 16:02:12 +00:00
list_for_each_entry_safe(child, dt, &old_dev->children, sibling) {
dev_dbg(old_dev, "unregister child %s\n", dev_name(child));
unregister_device(child);
}
list_for_each_entry_safe(cdev, ct, &old_dev->cdevs, devices_list) {
if (cdev->flags & DEVFS_IS_PARTITION) {
dev_dbg(old_dev, "unregister part %s\n", cdev->name);
devfs_del_partition(cdev->name);
}
}
list_del(&old_dev->list);
list_del(&old_dev->bus_list);
list_del(&old_dev->active);
/* remove device from parents child list */
if (old_dev->parent)
list_del(&old_dev->sibling);
return 0;
2007-07-05 16:02:12 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(unregister_device);
2007-07-05 16:02:12 +00:00
struct driver_d *get_driver_by_name(const char *name)
{
struct driver_d *drv;
2007-07-05 16:02:12 +00:00
for_each_driver(drv) {
if(!strcmp(name, drv->name))
return drv;
2007-07-05 16:02:12 +00:00
}
return NULL;
2007-07-05 16:02:12 +00:00
}
int register_driver(struct driver_d *drv)
2007-07-05 16:02:12 +00:00
{
2007-10-11 18:56:18 +00:00
struct device_d *dev = NULL;
2007-07-05 16:02:12 +00:00
debug("register_driver: %s\n", drv->name);
2007-07-05 16:02:12 +00:00
BUG_ON(!drv->bus);
list_add_tail(&drv->list, &driver_list);
list_add_tail(&drv->bus_list, &drv->bus->driver_list);
2007-07-05 16:02:12 +00:00
bus_for_each_device(drv->bus, dev)
2007-10-11 18:56:18 +00:00
match(drv, dev);
2007-07-05 16:02:12 +00:00
return 0;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(register_driver);
2007-07-05 16:02:12 +00:00
struct resource *dev_get_resource(struct device_d *dev, unsigned long type,
int num)
{
int i, n = 0;
for (i = 0; i < dev->num_resources; i++) {
struct resource *res = &dev->resource[i];
if (resource_type(res) == type) {
if (n == num)
return res;
n++;
}
}
return NULL;
}
void *dev_get_mem_region(struct device_d *dev, int num)
{
struct resource *res;
res = dev_get_resource(dev, IORESOURCE_MEM, num);
if (!res)
return NULL;
return (void __force *)res->start;
}
EXPORT_SYMBOL(dev_get_mem_region);
struct resource *dev_get_resource_by_name(struct device_d *dev,
unsigned long type,
const char *name)
{
int i;
for (i = 0; i < dev->num_resources; i++) {
struct resource *res = &dev->resource[i];
if (resource_type(res) != type)
continue;
if (!res->name)
continue;
if (!strcmp(name, res->name))
return res;
}
return NULL;
}
void *dev_get_mem_region_by_name(struct device_d *dev, const char *name)
{
struct resource *res;
res = dev_get_resource_by_name(dev, IORESOURCE_MEM, name);
if (!res)
return NULL;
return (void __force *)res->start;
}
EXPORT_SYMBOL(dev_get_mem_region_by_name);
void __iomem *dev_request_mem_region_by_name(struct device_d *dev, const char *name)
{
struct resource *res;
res = dev_get_resource_by_name(dev, IORESOURCE_MEM, name);
if (!res)
return NULL;
res = request_iomem_region(dev_name(dev), res->start, res->end);
if (!res)
return NULL;
return (void __force __iomem *)res->start;
}
EXPORT_SYMBOL(dev_request_mem_region_by_name);
void __iomem *dev_request_mem_region(struct device_d *dev, int num)
{
struct resource *res;
res = dev_get_resource(dev, IORESOURCE_MEM, num);
if (!res)
return NULL;
res = request_iomem_region(dev_name(dev), res->start, res->end);
if (!res)
return NULL;
return (void __force __iomem *)res->start;
}
EXPORT_SYMBOL(dev_request_mem_region);
2007-07-16 08:29:28 +00:00
int dev_protect(struct device_d *dev, size_t count, unsigned long offset, int prot)
{
printf("%s: currently broken\n", __func__);
return -EINVAL;
2007-07-16 08:29:28 +00:00
}
int generic_memmap_ro(struct cdev *cdev, void **map, int flags)
{
if (!cdev->dev)
return -EINVAL;
if (flags & PROT_WRITE)
return -EACCES;
*map = dev_get_mem_region(cdev->dev, 0);
return 0;
}
int generic_memmap_rw(struct cdev *cdev, void **map, int flags)
{
if (!cdev->dev)
return -EINVAL;
*map = dev_get_mem_region(cdev->dev, 0);
return 0;
}
2007-07-05 16:02:12 +00:00
int dummy_probe(struct device_d *dev)
{
2007-10-11 18:56:18 +00:00
return 0;
2007-07-05 16:02:12 +00:00
}
EXPORT_SYMBOL(dummy_probe);
2007-07-05 16:02:12 +00:00
const char *dev_id(const struct device_d *dev)
{
static char buf[MAX_DRIVER_NAME + 16];
if (dev->id != DEVICE_ID_SINGLE)
snprintf(buf, sizeof(buf), FORMAT_DRIVER_NAME_ID, dev->name, dev->id);
else
snprintf(buf, sizeof(buf), "%s", dev->name);
return buf;
}
int dev_printf(int level, const struct device_d *dev, const char *format, ...)
{
va_list args;
int ret = 0;
if (level > barebox_loglevel)
return 0;
if (dev->driver && dev->driver->name)
ret += printf("%s ", dev->driver->name);
ret += printf("%s: ", dev_name(dev));
va_start(args, format);
ret += vprintf(format, args);
va_end(args);
return ret;
}
void devices_shutdown(void)
{
struct device_d *dev;
list_for_each_entry(dev, &active, active) {
if (dev->driver->remove)
dev->driver->remove(dev);
}
}
int dev_get_drvdata(struct device_d *dev, unsigned long *data)
{
if (dev->of_id_entry) {
*data = dev->of_id_entry->data;
return 0;
}
if (dev->id_entry) {
*data = dev->id_entry->driver_data;
return 0;
}
return -ENODEV;
}