[PPC] Move include/asm-ppc/arch-* to arch/ppc/*/include/mach
[barebox-mini2440.git] / include / driver.h
blob2ee7fb1cf8365491a32bf9e7f2993568b5fcfe41
1 /*
2 * (C) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
4 * See file CREDITS for list of people who contributed to this
5 * project.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
23 #ifndef DRIVER_H
24 #define DRIVER_H
26 #include <list.h>
28 #define MAX_DRIVER_NAME 32
30 #include <param.h>
32 /**
33 * @file
34 * @brief Main description of the device/driver model
37 /** @page driver_model Main description of the device/driver model
39 * We follow a rather simplistic driver model here. There is a
40 * @code struct device_d @endcode
41 * which describes a particular device present in the system.
43 * On the other side a
44 * @code struct driver_d @endcode
45 * represents a driver present in the system.
47 * Both structs find together via the members 'type' (int) and 'name' (char *).
48 * If both members match, the driver's probe function is called with the
49 * struct device_d as argument.
51 * People familiar with the Linux platform bus will recognize this behaviour
52 * and in fact many things were stolen from there. Some selected members of the
53 * structs will be described in this document.
56 /*@{*/ /* do not delete, doxygen relevant */
58 struct filep;
59 struct bus_type;
61 /** @brief Describes a particular device present in the system */
62 struct device_d {
63 /*! This member (and 'type' described below) is used to match with a
64 * driver. This is a descriptive name and could be MPC5XXX_ether or
65 * imx_serial. */
66 char name[MAX_DRIVER_NAME];
67 /*! The id is used to uniquely identify a device in the system. The id
68 * will show up under /dev/ as the device's name. Usually this is
69 * something like eth0 or nor0. */
70 int id;
72 /*! FIXME */
73 unsigned long size;
75 /*! For devices which are directly mapped into memory, i.e. NOR
76 * Flash or SDRAM. */
77 unsigned long map_base;
79 void *platform_data; /*! board specific information about this device */
81 /*! Devices of a particular class normaly need to store more
82 * information than struct device holds.
84 void *priv;
85 void *type_data; /*! In case this device is a specific device, this pointer
86 * points to the type specific device, i.e. eth_device
88 struct driver_d *driver; /*! The driver for this device */
90 struct list_head list; /* The list of all devices */
91 struct list_head children; /* our children */
92 struct list_head sibling;
93 struct list_head active; /* The list of all devices which have a driver */
95 struct device_d *parent; /* our parent, NULL if not present */
97 struct bus_type *bus;
99 /*! The parameters for this device. This is used to carry information
100 * of board specific data from the board code to the device driver. */
101 struct param_d *param;
103 struct list_head cdevs;
106 /** @brief Describes a driver present in the system */
107 struct driver_d {
108 /*! The name of this driver. Used to match to
109 * the corresponding device. */
110 char name[MAX_DRIVER_NAME];
112 struct list_head list;
114 /*! Called if an instance of a device is found */
115 int (*probe) (struct device_d *);
117 /*! Called if an instance of a device is gone. */
118 void (*remove)(struct device_d *);
120 void (*info) (struct device_d *);
121 void (*shortinfo) (struct device_d *);
123 unsigned long type;
124 struct bus_type *bus;
126 /*! This is somewhat redundant with the type data in struct device.
127 * Currently the filesystem implementation uses this field while
128 * ethernet drivers use the same field in struct device. Probably
129 * one of both should be removed. */
130 void *type_data;
133 /*@}*/ /* do not delete, doxygen relevant */
135 #define RW_SIZE(x) (x)
136 #define RW_SIZE_MASK 0x7
138 /* Register devices and drivers.
140 int register_driver(struct driver_d *);
141 int register_device(struct device_d *);
143 /* Unregister a device. This function can fail, e.g. when the device
144 * has children.
146 int unregister_device(struct device_d *);
148 /* Organize devices in a tree. These functions do _not_ register or
149 * unregister a device. Only registered devices are allowed here.
151 int dev_add_child(struct device_d *dev, struct device_d *child);
153 /* Iterate over a devices children
155 #define device_for_each_child(dev, child) \
156 list_for_each_entry(child, &dev->children, sibling)
158 /* Iterate over a devices children - Safe against removal version
160 #define device_for_each_child_safe(dev, tmpdev, child) \
161 list_for_each_entry_safe(child, tmpdev, &dev->children, sibling)
163 /* Iterate through the devices of a given type. if last is NULL, the
164 * first device of this type is returned. Put this pointer in as
165 * 'last' to get the next device. This functions returns NULL if no
166 * more devices are found.
168 struct device_d *get_device_by_type(ulong type, struct device_d *last);
169 struct device_d *get_device_by_id(const char *id);
170 struct device_d *get_device_by_name(const char *name);
172 /* Find a free device id from the given template. This is archieved by
173 * appending a number to the template. Dynamically created devices should
174 * use this function rather than filling the id field themselves.
176 int get_free_deviceid(const char *name_template);
178 char *deviceid_from_spec_str(const char *str, char **endp);
180 /* linear list over all available devices
182 extern struct list_head device_list;
184 /* linear list over all available drivers
186 extern struct list_head driver_list;
188 /* Iterate over all devices
190 #define for_each_device(dev) list_for_each_entry(dev, &device_list, list)
192 /* Iterate over all drivers
194 #define for_each_driver(drv) list_for_each_entry(drv, &driver_list, list)
196 /* Find a driver with the given name. Currently the filesystem implementation
197 * uses this to get the driver from the name the user specifies with the
198 * mount command
200 struct driver_d *get_driver_by_name(const char *name);
202 struct cdev;
204 int dev_protect(struct device_d *dev, size_t count, unsigned long offset, int prot);
206 /* These are used by drivers which work with direct memory accesses */
207 ssize_t mem_read(struct cdev *cdev, void *buf, size_t count, ulong offset, ulong flags);
208 ssize_t mem_write(struct cdev *cdev, const void *buf, size_t count, ulong offset, ulong flags);
209 int mem_memmap(struct cdev *cdev, void **map, int flags);
211 /* Use this if you have nothing to do in your drivers probe function */
212 int dummy_probe(struct device_d *);
214 /* Iterate over all activated devices (i.e. the ones with drivers and shut
215 * them down.
217 void devices_shutdown(void);
219 int generic_memmap_ro(struct cdev *dev, void **map, int flags);
220 int generic_memmap_rw(struct cdev *dev, void **map, int flags);
222 static inline off_t dev_lseek_default(struct cdev *cdev, off_t ofs)
224 return ofs;
227 static inline int dev_open_default(struct device_d *dev, struct filep *f)
229 return 0;
232 static inline int dev_close_default(struct device_d *dev, struct filep *f)
234 return 0;
237 /* debugging and troubleshooting/diagnostic helpers. */
238 extern const char *dev_id(const struct device_d *dev);
240 #define dev_printf(dev, format, arg...) \
241 printf("%s@%s: " format , (dev)->name , \
242 dev_id(dev) , ## arg)
244 #define dev_emerg(dev, format, arg...) \
245 dev_printf((dev) , format , ## arg)
246 #define dev_alert(dev, format, arg...) \
247 dev_printf((dev) , format , ## arg)
248 #define dev_crit(dev, format, arg...) \
249 dev_printf((dev) , format , ## arg)
250 #define dev_err(dev, format, arg...) \
251 dev_printf(dev , format , ## arg)
252 #define dev_warn(dev, format, arg...) \
253 dev_printf((dev) , format , ## arg)
254 #define dev_notice(dev, format, arg...) \
255 dev_printf((dev) , format , ## arg)
256 #define dev_info(dev, format, arg...) \
257 dev_printf((dev) , format , ## arg)
259 #if defined(DEBUG)
260 #define dev_dbg(dev, format, arg...) \
261 dev_printf((dev) , format , ## arg)
262 #else
263 #define dev_dbg(dev, format, arg...) \
264 ({ if (0) dev_printf((dev), format, ##arg); 0; })
265 #endif
267 struct bus_type {
268 char *name;
269 int (*match)(struct device_d *dev, struct driver_d *drv);
270 int (*probe)(struct device_d *dev);
271 void (*remove)(struct device_d *dev);
273 struct list_head list;
276 extern struct bus_type platform_bus;
278 struct file_operations {
279 /*! Called in response of reading from this device. Required */
280 ssize_t (*read)(struct cdev*, void* buf, size_t count, ulong offset, ulong flags);
282 /*! Called in response of write to this device. Required */
283 ssize_t (*write)(struct cdev*, const void* buf, size_t count, ulong offset, ulong flags);
285 int (*ioctl)(struct cdev*, int, void *);
286 off_t (*lseek)(struct cdev*, off_t);
287 int (*open)(struct cdev*, struct filep*);
288 int (*close)(struct cdev*, struct filep*);
289 int (*erase)(struct cdev*, size_t count, unsigned long offset);
290 int (*protect)(struct cdev*, size_t count, unsigned long offset, int prot);
291 int (*memmap)(struct cdev*, void **map, int flags);
294 struct cdev {
295 struct file_operations *ops;
296 void *priv;
297 struct device_d *dev;
298 struct list_head list;
299 struct list_head devices_list;
300 char *name;
301 unsigned long offset;
302 size_t size;
303 unsigned int flags;
304 int open;
307 int devfs_create(struct cdev *);
308 int devfs_remove(struct cdev *);
309 struct cdev *cdev_by_name(const char *filename);
310 ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, ulong offset, ulong flags);
311 ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, ulong offset, ulong flags);
313 #define DEVFS_PARTITION_FIXED (1 << 0)
314 #define DEVFS_PARTITION_READONLY (1 << 1)
315 #define DEVFS_IS_PARTITION (1 << 2)
316 #define DEVFS_RDWR (1 << 3)
318 int devfs_add_partition(const char *devname, unsigned long offset, size_t size,
319 int flags, const char *name);
320 int devfs_del_partition(const char *name);
322 struct memory_platform_data {
323 char *name;
324 unsigned int flags;
327 #endif /* DRIVER_H */