diff --git a/include/driver.h b/include/driver.h index a29ede409..b2ab7c25a 100644 --- a/include/driver.h +++ b/include/driver.h @@ -68,6 +68,7 @@ /*@{*/ /* do not delete, doxygen relevant */ struct filep; +struct bus_type; /** @brief Describes a particular device present in the system */ struct device_d { @@ -110,6 +111,7 @@ struct device_d { /*! This describes the type (or class) of this device. Have a look at * include/driver.h to see a list of known device types. Currently this * includes DEVICE_TYPE_ETHER, DEVICE_TYPE_CONSOLE and others. */ + struct bus_type *bus; unsigned long type; /*! The parameters for this device. This is used to carry information @@ -151,6 +153,7 @@ struct driver_d { void (*shortinfo) (struct device_d *); unsigned long type; + struct bus_type *bus; /*! This is somewhat redundant with the type data in struct device. * Currently the filesystem implementation uses this field while @@ -273,7 +276,7 @@ static inline int dev_close_default(struct device_d *dev, struct filep *f) extern const char *dev_id(const struct device_d *dev); #define dev_printf(dev, format, arg...) \ - printf("%s@%s: " format , dev->name , \ + printf("%s@%s: " format , (dev)->name , \ dev_id(dev) , ## arg) #define dev_emerg(dev, format, arg...) \ @@ -299,5 +302,15 @@ extern const char *dev_id(const struct device_d *dev); ({ if (0) dev_printf((dev), format, ##arg); 0; }) #endif +struct bus_type { + char *name; + int (*match)(struct device_d *dev, struct driver_d *drv); + int (*probe)(struct device_d *dev); + void (*remove)(struct device_d *dev); + + struct list_head list; +}; + +extern struct bus_type platform_bus; #endif /* DRIVER_H */ diff --git a/lib/Makefile b/lib/Makefile index 2828fbe11..c52b06fac 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -6,6 +6,7 @@ obj-y += vsprintf.o obj-y += div64.o obj-y += misc.o obj-y += driver.o +obj-y += bus.o obj-y += parameter.o obj-y += xfuncs.o obj-y += getopt.o diff --git a/lib/bus.c b/lib/bus.c new file mode 100644 index 000000000..39170fe38 --- /dev/null +++ b/lib/bus.c @@ -0,0 +1,58 @@ +/* + * bus.c - U-Boot driver model + * + * Copyright (c) 2009 Sascha Hauer , 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include + +static int platform_match(struct device_d *dev, struct driver_d *drv) +{ + return strcmp(dev->name, drv->name) ? -1 : 0; +} + +static int platform_probe(struct device_d *dev) +{ + return dev->driver->probe(dev); +} + +static int platform_remove(struct device_d *dev) +{ + return dev->driver->remove(dev); +} + +struct bus_type platform_bus = { + .name = "platform", + .match = platform_match, + .probe = platform_probe, + .remove = platform_remove, +}; + +#if 0 +LIST_HEAD(bus_list); +EXPORT_SYMBOL(bus_list); + +int bus_register(struct bus_type *bus) +{ + list_add_tail(&bus->list, &bus_list); + + return 0; +} +#endif + diff --git a/lib/driver.c b/lib/driver.c index 9f0b14945..6f49ca97d 100644 --- a/lib/driver.c +++ b/lib/driver.c @@ -70,18 +70,21 @@ int get_free_deviceid(char *id, const char *id_template) static int match(struct driver_d *drv, struct device_d *dev) { - if (strcmp(dev->name, drv->name)) - return -1; - if (dev->type != drv->type) - return -1; - if(drv->probe(dev)) - return -1; - dev->driver = drv; + if (dev->bus != drv->bus) + goto err_out; + if (dev->bus->match(dev, drv)) + goto err_out; + if (dev->bus->probe(dev)) + goto err_out; + list_add(&dev->active, &active); return 0; +err_out: + dev->driver = NULL; + return -1; } int register_device(struct device_d *new_device) @@ -94,6 +97,11 @@ int register_device(struct device_d *new_device) } debug ("register_device: %s\n",new_device->name); + if (!new_device->bus) { +// dev_err(new_device, "no bus type associated. Needs fixup\n"); + new_device->bus = &platform_bus; + } + list_add_tail(&new_device->list, &device_list); INIT_LIST_HEAD(&new_device->children); @@ -165,6 +173,11 @@ int register_driver(struct driver_d *drv) debug("register_driver: %s\n", drv->name); + if (!drv->bus) { +// pr_err("driver %s has no bus type associated. Needs fixup\n", drv->name); + drv->bus = &platform_bus; + } + list_add_tail(&drv->list, &driver_list); if (!drv->info)