blob: 07222c531d377849925dbdef25e2501e59f3d0ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * device.h - generic, centralized driver model
3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 *
6 * This file is released under the GPLv2
7 *
8 * See Documentation/driver-model/ for more information.
9 */
10
11#ifndef _DEVICE_H_
12#define _DEVICE_H_
13
14#include <linux/config.h>
15#include <linux/ioport.h>
16#include <linux/kobject.h>
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -080017#include <linux/klist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/list.h>
19#include <linux/types.h>
20#include <linux/module.h>
21#include <linux/pm.h>
22#include <asm/semaphore.h>
23#include <asm/atomic.h>
24
25#define DEVICE_NAME_SIZE 50
26#define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */
27#define DEVICE_ID_SIZE 32
28#define BUS_ID_SIZE KOBJ_NAME_LEN
29
30
31enum {
32 SUSPEND_NOTIFY,
33 SUSPEND_SAVE_STATE,
34 SUSPEND_DISABLE,
35 SUSPEND_POWER_DOWN,
36};
37
38enum {
39 RESUME_POWER_ON,
40 RESUME_RESTORE_STATE,
41 RESUME_ENABLE,
42};
43
44struct device;
45struct device_driver;
46struct class;
47struct class_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49struct bus_type {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -050050 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 struct subsystem subsys;
53 struct kset drivers;
54 struct kset devices;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -080055 struct klist klist_devices;
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -080056 struct klist klist_drivers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 struct bus_attribute * bus_attrs;
59 struct device_attribute * dev_attrs;
60 struct driver_attribute * drv_attrs;
61
62 int (*match)(struct device * dev, struct device_driver * drv);
63 int (*hotplug) (struct device *dev, char **envp,
64 int num_envp, char *buffer, int buffer_size);
65 int (*suspend)(struct device * dev, pm_message_t state);
66 int (*resume)(struct device * dev);
67};
68
69extern int bus_register(struct bus_type * bus);
70extern void bus_unregister(struct bus_type * bus);
71
72extern int bus_rescan_devices(struct bus_type * bus);
73
74extern struct bus_type * get_bus(struct bus_type * bus);
75extern void put_bus(struct bus_type * bus);
76
77extern struct bus_type * find_bus(char * name);
78
79/* iterator helpers for buses */
80
81int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
82 int (*fn)(struct device *, void *));
Cornelia Huck0edb5862005-06-22 16:59:51 +020083struct device * bus_find_device(struct bus_type *bus, struct device *start,
84 void *data, int (*match)(struct device *, void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
87 void * data, int (*fn)(struct device_driver *, void *));
88
89
90/* driverfs interface for exporting bus attributes */
91
92struct bus_attribute {
93 struct attribute attr;
94 ssize_t (*show)(struct bus_type *, char * buf);
95 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
96};
97
98#define BUS_ATTR(_name,_mode,_show,_store) \
99struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
100
101extern int bus_create_file(struct bus_type *, struct bus_attribute *);
102extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
103
104struct device_driver {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500105 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct bus_type * bus;
107
108 struct completion unloaded;
109 struct kobject kobj;
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800110 struct klist klist_devices;
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800111 struct klist_node knode_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500113 struct module * owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 int (*probe) (struct device * dev);
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500116 int (*remove) (struct device * dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 void (*shutdown) (struct device * dev);
118 int (*suspend) (struct device * dev, pm_message_t state, u32 level);
119 int (*resume) (struct device * dev, u32 level);
120};
121
122
123extern int driver_register(struct device_driver * drv);
124extern void driver_unregister(struct device_driver * drv);
125
126extern struct device_driver * get_driver(struct device_driver * drv);
127extern void put_driver(struct device_driver * drv);
128extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
129
130
131/* driverfs interface for exporting driver attributes */
132
133struct driver_attribute {
134 struct attribute attr;
135 ssize_t (*show)(struct device_driver *, char * buf);
136 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
137};
138
139#define DRIVER_ATTR(_name,_mode,_show,_store) \
140struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
141
142extern int driver_create_file(struct device_driver *, struct driver_attribute *);
143extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
144
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -0800145extern int driver_for_each_device(struct device_driver * drv, struct device * start,
146 void * data, int (*fn)(struct device *, void *));
Cornelia Huck0edb5862005-06-22 16:59:51 +0200147struct device * driver_find_device(struct device_driver *drv,
148 struct device *start, void *data,
149 int (*match)(struct device *, void *));
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/*
153 * device classes
154 */
155struct class {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500156 const char * name;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800157 struct module * owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 struct subsystem subsys;
160 struct list_head children;
161 struct list_head interfaces;
162 struct semaphore sem; /* locks both the children and interfaces lists */
163
164 struct class_attribute * class_attrs;
165 struct class_device_attribute * class_dev_attrs;
166
167 int (*hotplug)(struct class_device *dev, char **envp,
168 int num_envp, char *buffer, int buffer_size);
169
170 void (*release)(struct class_device *dev);
171 void (*class_release)(struct class *class);
172};
173
174extern int class_register(struct class *);
175extern void class_unregister(struct class *);
176
177extern struct class * class_get(struct class *);
178extern void class_put(struct class *);
179
180
181struct class_attribute {
182 struct attribute attr;
183 ssize_t (*show)(struct class *, char * buf);
184 ssize_t (*store)(struct class *, const char * buf, size_t count);
185};
186
187#define CLASS_ATTR(_name,_mode,_show,_store) \
188struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
189
190extern int class_create_file(struct class *, const struct class_attribute *);
191extern void class_remove_file(struct class *, const struct class_attribute *);
192
193
194struct class_device {
195 struct list_head node;
196
197 struct kobject kobj;
198 struct class * class; /* required */
199 dev_t devt; /* dev_t, creates the sysfs "dev" */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800200 struct class_device_attribute *devt_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct device * dev; /* not necessary, but nice to have */
202 void * class_data; /* class-specific data */
203
204 char class_id[BUS_ID_SIZE]; /* unique to this class */
205};
206
207static inline void *
208class_get_devdata (struct class_device *dev)
209{
210 return dev->class_data;
211}
212
213static inline void
214class_set_devdata (struct class_device *dev, void *data)
215{
216 dev->class_data = data;
217}
218
219
220extern int class_device_register(struct class_device *);
221extern void class_device_unregister(struct class_device *);
222extern void class_device_initialize(struct class_device *);
223extern int class_device_add(struct class_device *);
224extern void class_device_del(struct class_device *);
225
226extern int class_device_rename(struct class_device *, char *);
227
228extern struct class_device * class_device_get(struct class_device *);
229extern void class_device_put(struct class_device *);
230
231struct class_device_attribute {
232 struct attribute attr;
233 ssize_t (*show)(struct class_device *, char * buf);
234 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
235};
236
237#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
238struct class_device_attribute class_device_attr_##_name = \
239 __ATTR(_name,_mode,_show,_store)
240
241extern int class_device_create_file(struct class_device *,
242 const struct class_device_attribute *);
243extern void class_device_remove_file(struct class_device *,
244 const struct class_device_attribute *);
245extern int class_device_create_bin_file(struct class_device *,
246 struct bin_attribute *);
247extern void class_device_remove_bin_file(struct class_device *,
248 struct bin_attribute *);
249
250struct class_interface {
251 struct list_head node;
252 struct class *class;
253
254 int (*add) (struct class_device *);
255 void (*remove) (struct class_device *);
256};
257
258extern int class_interface_register(struct class_interface *);
259extern void class_interface_unregister(struct class_interface *);
260
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800261extern struct class *class_create(struct module *owner, char *name);
262extern void class_destroy(struct class *cls);
263extern struct class_device *class_device_create(struct class *cls, dev_t devt,
264 struct device *device, char *fmt, ...)
265 __attribute__((format(printf,4,5)));
266extern void class_device_destroy(struct class *cls, dev_t devt);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269struct device {
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800270 struct klist klist_children;
271 struct klist_node knode_parent; /* node in sibling list */
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800272 struct klist_node knode_driver;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800273 struct klist_node knode_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct device * parent;
275
276 struct kobject kobj;
277 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
278
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800279 struct semaphore sem; /* semaphore to synchronize calls to
280 * its driver.
281 */
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct bus_type * bus; /* type of bus device is on */
284 struct device_driver *driver; /* which driver has allocated this
285 device */
286 void *driver_data; /* data private to the driver */
287 void *platform_data; /* Platform specific data (e.g. ACPI,
288 BIOS data relevant to device) */
289 struct dev_pm_info power;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u64 *dma_mask; /* dma mask (if dma'able device) */
292 u64 coherent_dma_mask;/* Like dma_mask, but for
293 alloc_coherent mappings as
294 not all hardware supports
295 64 bit addresses for consistent
296 allocations such descriptors. */
297
298 struct list_head dma_pools; /* dma pools (if dma'ble) */
299
300 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
301 override */
302
303 void (*release)(struct device * dev);
304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static inline void *
307dev_get_drvdata (struct device *dev)
308{
309 return dev->driver_data;
310}
311
312static inline void
313dev_set_drvdata (struct device *dev, void *data)
314{
315 dev->driver_data = data;
316}
317
318/*
319 * High level routines for use by the bus drivers
320 */
321extern int device_register(struct device * dev);
322extern void device_unregister(struct device * dev);
323extern void device_initialize(struct device * dev);
324extern int device_add(struct device * dev);
325extern void device_del(struct device * dev);
326extern int device_for_each_child(struct device *, void *,
327 int (*fn)(struct device *, void *));
328
329/*
330 * Manual binding of a device to driver. See drivers/base/bus.c
331 * for information on use.
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333extern void device_bind_driver(struct device * dev);
334extern void device_release_driver(struct device * dev);
335extern int device_attach(struct device * dev);
336extern void driver_attach(struct device_driver * drv);
337
338
339/* driverfs interface for exporting device attributes */
340
341struct device_attribute {
342 struct attribute attr;
Yani Ioannou54b6f352005-05-17 06:39:34 -0400343 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
344 char *buf);
345 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349#define DEVICE_ATTR(_name,_mode,_show,_store) \
350struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
351
352
353extern int device_create_file(struct device *device, struct device_attribute * entry);
354extern void device_remove_file(struct device * dev, struct device_attribute * attr);
355
356/*
357 * Platform "fixup" functions - allow the platform to have their say
358 * about devices and actions that the general device layer doesn't
359 * know about.
360 */
361/* Notify platform of device discovery */
362extern int (*platform_notify)(struct device * dev);
363
364extern int (*platform_notify_remove)(struct device * dev);
365
366
367/**
368 * get_device - atomically increment the reference count for the device.
369 *
370 */
371extern struct device * get_device(struct device * dev);
372extern void put_device(struct device * dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374
375/* drivers/base/platform.c */
376
377struct platform_device {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500378 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u32 id;
380 struct device dev;
381 u32 num_resources;
382 struct resource * resource;
383};
384
385#define to_platform_device(x) container_of((x), struct platform_device, dev)
386
387extern int platform_device_register(struct platform_device *);
388extern void platform_device_unregister(struct platform_device *);
389
390extern struct bus_type platform_bus_type;
391extern struct device platform_bus;
392
393extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
394extern int platform_get_irq(struct platform_device *, unsigned int);
395extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
396extern int platform_get_irq_byname(struct platform_device *, char *);
397extern int platform_add_devices(struct platform_device **, int);
398
399extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
400
401/* drivers/base/power.c */
402extern void device_shutdown(void);
403
404
405/* drivers/base/firmware.c */
406extern int firmware_register(struct subsystem *);
407extern void firmware_unregister(struct subsystem *);
408
409/* debugging and troubleshooting/diagnostic helpers. */
410#define dev_printk(level, dev, format, arg...) \
411 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
412
413#ifdef DEBUG
414#define dev_dbg(dev, format, arg...) \
415 dev_printk(KERN_DEBUG , dev , format , ## arg)
416#else
417#define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
418#endif
419
420#define dev_err(dev, format, arg...) \
421 dev_printk(KERN_ERR , dev , format , ## arg)
422#define dev_info(dev, format, arg...) \
423 dev_printk(KERN_INFO , dev , format , ## arg)
424#define dev_warn(dev, format, arg...) \
425 dev_printk(KERN_WARNING , dev , format , ## arg)
426
427/* Create alias, so I can be autoloaded. */
428#define MODULE_ALIAS_CHARDEV(major,minor) \
429 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
430#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
431 MODULE_ALIAS("char-major-" __stringify(major) "-*")
432#endif /* _DEVICE_H_ */