Merge tag 'v3.10.72' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / include / linux / device.h
1 /*
2  * device.h - generic, centralized driver model
3  *
4  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
6  * Copyright (c) 2008-2009 Novell Inc.
7  *
8  * This file is released under the GPLv2
9  *
10  * See Documentation/driver-model/ for more information.
11  */
12
13 #ifndef _DEVICE_H_
14 #define _DEVICE_H_
15
16 #include <linux/ioport.h>
17 #include <linux/kobject.h>
18 #include <linux/klist.h>
19 #include <linux/list.h>
20 #include <linux/lockdep.h>
21 #include <linux/compiler.h>
22 #include <linux/types.h>
23 #include <linux/mutex.h>
24 #include <linux/pinctrl/devinfo.h>
25 #include <linux/pm.h>
26 #include <linux/atomic.h>
27 #include <linux/ratelimit.h>
28 #include <linux/uidgid.h>
29 #include <linux/gfp.h>
30 #include <asm/device.h>
31
32 struct device;
33 struct device_private;
34 struct device_driver;
35 struct driver_private;
36 struct module;
37 struct class;
38 struct subsys_private;
39 struct bus_type;
40 struct device_node;
41 struct iommu_ops;
42 struct iommu_group;
43
44 struct bus_attribute {
45         struct attribute        attr;
46         ssize_t (*show)(struct bus_type *bus, char *buf);
47         ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
48 };
49
50 #define BUS_ATTR(_name, _mode, _show, _store)   \
51         struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
52 #define BUS_ATTR_RW(_name) \
53         struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
54 #define BUS_ATTR_RO(_name) \
55         struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
56
57 extern int __must_check bus_create_file(struct bus_type *,
58                                         struct bus_attribute *);
59 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
60
61 /**
62  * struct bus_type - The bus type of the device
63  *
64  * @name:       The name of the bus.
65  * @dev_name:   Used for subsystems to enumerate devices like ("foo%u", dev->id).
66  * @dev_root:   Default device to use as the parent.
67  * @bus_attrs:  Default attributes of the bus.
68  * @dev_attrs:  Default attributes of the devices on the bus.
69  * @drv_attrs:  Default attributes of the device drivers on the bus.
70  * @match:      Called, perhaps multiple times, whenever a new device or driver
71  *              is added for this bus. It should return a nonzero value if the
72  *              given device can be handled by the given driver.
73  * @uevent:     Called when a device is added, removed, or a few other things
74  *              that generate uevents to add the environment variables.
75  * @probe:      Called when a new device or driver add to this bus, and callback
76  *              the specific driver's probe to initial the matched device.
77  * @remove:     Called when a device removed from this bus.
78  * @shutdown:   Called at shut-down time to quiesce the device.
79  * @suspend:    Called when a device on this bus wants to go to sleep mode.
80  * @resume:     Called to bring a device on this bus out of sleep mode.
81  * @pm:         Power management operations of this bus, callback the specific
82  *              device driver's pm-ops.
83  * @iommu_ops:  IOMMU specific operations for this bus, used to attach IOMMU
84  *              driver implementations to a bus and allow the driver to do
85  *              bus-specific setup
86  * @p:          The private data of the driver core, only the driver core can
87  *              touch this.
88  *
89  * A bus is a channel between the processor and one or more devices. For the
90  * purposes of the device model, all devices are connected via a bus, even if
91  * it is an internal, virtual, "platform" bus. Buses can plug into each other.
92  * A USB controller is usually a PCI device, for example. The device model
93  * represents the actual connections between buses and the devices they control.
94  * A bus is represented by the bus_type structure. It contains the name, the
95  * default attributes, the bus' methods, PM operations, and the driver core's
96  * private data.
97  */
98 struct bus_type {
99         const char              *name;
100         const char              *dev_name;
101         struct device           *dev_root;
102         struct bus_attribute    *bus_attrs;
103         struct device_attribute *dev_attrs;
104         struct driver_attribute *drv_attrs;
105
106         int (*match)(struct device *dev, struct device_driver *drv);
107         int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
108         int (*probe)(struct device *dev);
109         int (*remove)(struct device *dev);
110         void (*shutdown)(struct device *dev);
111
112         int (*suspend)(struct device *dev, pm_message_t state);
113         int (*resume)(struct device *dev);
114
115         const struct dev_pm_ops *pm;
116
117         struct iommu_ops *iommu_ops;
118
119         struct subsys_private *p;
120         struct lock_class_key lock_key;
121 };
122
123 extern int __must_check bus_register(struct bus_type *bus);
124
125 extern void bus_unregister(struct bus_type *bus);
126
127 extern int __must_check bus_rescan_devices(struct bus_type *bus);
128
129 /* iterator helpers for buses */
130 struct subsys_dev_iter {
131         struct klist_iter               ki;
132         const struct device_type        *type;
133 };
134 void subsys_dev_iter_init(struct subsys_dev_iter *iter,
135                          struct bus_type *subsys,
136                          struct device *start,
137                          const struct device_type *type);
138 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
139 void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
140
141 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
142                      int (*fn)(struct device *dev, void *data));
143 struct device *bus_find_device(struct bus_type *bus, struct device *start,
144                                void *data,
145                                int (*match)(struct device *dev, void *data));
146 struct device *bus_find_device_by_name(struct bus_type *bus,
147                                        struct device *start,
148                                        const char *name);
149 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
150                                         struct device *hint);
151 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
152                      void *data, int (*fn)(struct device_driver *, void *));
153 void bus_sort_breadthfirst(struct bus_type *bus,
154                            int (*compare)(const struct device *a,
155                                           const struct device *b));
156 /*
157  * Bus notifiers: Get notified of addition/removal of devices
158  * and binding/unbinding of drivers to devices.
159  * In the long run, it should be a replacement for the platform
160  * notify hooks.
161  */
162 struct notifier_block;
163
164 extern int bus_register_notifier(struct bus_type *bus,
165                                  struct notifier_block *nb);
166 extern int bus_unregister_notifier(struct bus_type *bus,
167                                    struct notifier_block *nb);
168
169 /* All 4 notifers below get called with the target struct device *
170  * as an argument. Note that those functions are likely to be called
171  * with the device lock held in the core, so be careful.
172  */
173 #define BUS_NOTIFY_ADD_DEVICE           0x00000001 /* device added */
174 #define BUS_NOTIFY_DEL_DEVICE           0x00000002 /* device removed */
175 #define BUS_NOTIFY_BIND_DRIVER          0x00000003 /* driver about to be
176                                                       bound */
177 #define BUS_NOTIFY_BOUND_DRIVER         0x00000004 /* driver bound to device */
178 #define BUS_NOTIFY_UNBIND_DRIVER        0x00000005 /* driver about to be
179                                                       unbound */
180 #define BUS_NOTIFY_UNBOUND_DRIVER       0x00000006 /* driver is unbound
181                                                       from the device */
182
183 extern struct kset *bus_get_kset(struct bus_type *bus);
184 extern struct klist *bus_get_device_klist(struct bus_type *bus);
185
186 /**
187  * struct device_driver - The basic device driver structure
188  * @name:       Name of the device driver.
189  * @bus:        The bus which the device of this driver belongs to.
190  * @owner:      The module owner.
191  * @mod_name:   Used for built-in modules.
192  * @suppress_bind_attrs: Disables bind/unbind via sysfs.
193  * @of_match_table: The open firmware table.
194  * @acpi_match_table: The ACPI match table.
195  * @probe:      Called to query the existence of a specific device,
196  *              whether this driver can work with it, and bind the driver
197  *              to a specific device.
198  * @remove:     Called when the device is removed from the system to
199  *              unbind a device from this driver.
200  * @shutdown:   Called at shut-down time to quiesce the device.
201  * @suspend:    Called to put the device to sleep mode. Usually to a
202  *              low power state.
203  * @resume:     Called to bring a device from sleep mode.
204  * @groups:     Default attributes that get created by the driver core
205  *              automatically.
206  * @pm:         Power management operations of the device which matched
207  *              this driver.
208  * @p:          Driver core's private data, no one other than the driver
209  *              core can touch this.
210  *
211  * The device driver-model tracks all of the drivers known to the system.
212  * The main reason for this tracking is to enable the driver core to match
213  * up drivers with new devices. Once drivers are known objects within the
214  * system, however, a number of other things become possible. Device drivers
215  * can export information and configuration variables that are independent
216  * of any specific device.
217  */
218 struct device_driver {
219         const char              *name;
220         struct bus_type         *bus;
221
222         struct module           *owner;
223         const char              *mod_name;      /* used for built-in modules */
224
225         bool suppress_bind_attrs;       /* disables bind/unbind via sysfs */
226
227         const struct of_device_id       *of_match_table;
228         const struct acpi_device_id     *acpi_match_table;
229
230         int (*probe) (struct device *dev);
231         int (*remove) (struct device *dev);
232         void (*shutdown) (struct device *dev);
233         int (*suspend) (struct device *dev, pm_message_t state);
234         int (*resume) (struct device *dev);
235         const struct attribute_group **groups;
236
237         const struct dev_pm_ops *pm;
238
239         struct driver_private *p;
240 };
241
242
243 extern int __must_check driver_register(struct device_driver *drv);
244 extern void driver_unregister(struct device_driver *drv);
245
246 extern struct device_driver *driver_find(const char *name,
247                                          struct bus_type *bus);
248 extern int driver_probe_done(void);
249 extern void wait_for_device_probe(void);
250
251
252 /* sysfs interface for exporting driver attributes */
253
254 struct driver_attribute {
255         struct attribute attr;
256         ssize_t (*show)(struct device_driver *driver, char *buf);
257         ssize_t (*store)(struct device_driver *driver, const char *buf,
258                          size_t count);
259 };
260
261 #define DRIVER_ATTR(_name, _mode, _show, _store) \
262         struct driver_attribute driver_attr_##_name = __ATTR(_name, _mode, _show, _store)
263 #define DRIVER_ATTR_RW(_name) \
264         struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
265 #define DRIVER_ATTR_RO(_name) \
266         struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
267 #define DRIVER_ATTR_WO(_name) \
268         struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
269
270 extern int __must_check driver_create_file(struct device_driver *driver,
271                                         const struct driver_attribute *attr);
272 extern void driver_remove_file(struct device_driver *driver,
273                                const struct driver_attribute *attr);
274
275 extern int __must_check driver_for_each_device(struct device_driver *drv,
276                                                struct device *start,
277                                                void *data,
278                                                int (*fn)(struct device *dev,
279                                                          void *));
280 struct device *driver_find_device(struct device_driver *drv,
281                                   struct device *start, void *data,
282                                   int (*match)(struct device *dev, void *data));
283
284 /**
285  * struct subsys_interface - interfaces to device functions
286  * @name:       name of the device function
287  * @subsys:     subsytem of the devices to attach to
288  * @node:       the list of functions registered at the subsystem
289  * @add_dev:    device hookup to device function handler
290  * @remove_dev: device hookup to device function handler
291  *
292  * Simple interfaces attached to a subsystem. Multiple interfaces can
293  * attach to a subsystem and its devices. Unlike drivers, they do not
294  * exclusively claim or control devices. Interfaces usually represent
295  * a specific functionality of a subsystem/class of devices.
296  */
297 struct subsys_interface {
298         const char *name;
299         struct bus_type *subsys;
300         struct list_head node;
301         int (*add_dev)(struct device *dev, struct subsys_interface *sif);
302         int (*remove_dev)(struct device *dev, struct subsys_interface *sif);
303 };
304
305 int subsys_interface_register(struct subsys_interface *sif);
306 void subsys_interface_unregister(struct subsys_interface *sif);
307
308 int subsys_system_register(struct bus_type *subsys,
309                            const struct attribute_group **groups);
310 int subsys_virtual_register(struct bus_type *subsys,
311                             const struct attribute_group **groups);
312
313 /**
314  * struct class - device classes
315  * @name:       Name of the class.
316  * @owner:      The module owner.
317  * @class_attrs: Default attributes of this class.
318  * @dev_attrs:  Default attributes of the devices belong to the class.
319  * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
320  * @dev_kobj:   The kobject that represents this class and links it into the hierarchy.
321  * @dev_uevent: Called when a device is added, removed from this class, or a
322  *              few other things that generate uevents to add the environment
323  *              variables.
324  * @devnode:    Callback to provide the devtmpfs.
325  * @class_release: Called to release this class.
326  * @dev_release: Called to release the device.
327  * @suspend:    Used to put the device to sleep mode, usually to a low power
328  *              state.
329  * @resume:     Used to bring the device from the sleep mode.
330  * @ns_type:    Callbacks so sysfs can detemine namespaces.
331  * @namespace:  Namespace of the device belongs to this class.
332  * @pm:         The default device power management operations of this class.
333  * @p:          The private data of the driver core, no one other than the
334  *              driver core can touch this.
335  *
336  * A class is a higher-level view of a device that abstracts out low-level
337  * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
338  * at the class level, they are all simply disks. Classes allow user space
339  * to work with devices based on what they do, rather than how they are
340  * connected or how they work.
341  */
342 struct class {
343         const char              *name;
344         struct module           *owner;
345
346         struct class_attribute          *class_attrs;
347         struct device_attribute         *dev_attrs;
348         struct bin_attribute            *dev_bin_attrs;
349         struct kobject                  *dev_kobj;
350
351         int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
352         char *(*devnode)(struct device *dev, umode_t *mode);
353
354         void (*class_release)(struct class *class);
355         void (*dev_release)(struct device *dev);
356
357         int (*suspend)(struct device *dev, pm_message_t state);
358         int (*resume)(struct device *dev);
359
360         const struct kobj_ns_type_operations *ns_type;
361         const void *(*namespace)(struct device *dev);
362
363         const struct dev_pm_ops *pm;
364
365         struct subsys_private *p;
366 };
367
368 struct class_dev_iter {
369         struct klist_iter               ki;
370         const struct device_type        *type;
371 };
372
373 extern struct kobject *sysfs_dev_block_kobj;
374 extern struct kobject *sysfs_dev_char_kobj;
375 extern int __must_check __class_register(struct class *class,
376                                          struct lock_class_key *key);
377 extern void class_unregister(struct class *class);
378
379 /* This is a #define to keep the compiler from merging different
380  * instances of the __key variable */
381 #define class_register(class)                   \
382 ({                                              \
383         static struct lock_class_key __key;     \
384         __class_register(class, &__key);        \
385 })
386
387 struct class_compat;
388 struct class_compat *class_compat_register(const char *name);
389 void class_compat_unregister(struct class_compat *cls);
390 int class_compat_create_link(struct class_compat *cls, struct device *dev,
391                              struct device *device_link);
392 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
393                               struct device *device_link);
394
395 extern void class_dev_iter_init(struct class_dev_iter *iter,
396                                 struct class *class,
397                                 struct device *start,
398                                 const struct device_type *type);
399 extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
400 extern void class_dev_iter_exit(struct class_dev_iter *iter);
401
402 extern int class_for_each_device(struct class *class, struct device *start,
403                                  void *data,
404                                  int (*fn)(struct device *dev, void *data));
405 extern struct device *class_find_device(struct class *class,
406                                         struct device *start, const void *data,
407                                         int (*match)(struct device *, const void *));
408
409 struct class_attribute {
410         struct attribute attr;
411         ssize_t (*show)(struct class *class, struct class_attribute *attr,
412                         char *buf);
413         ssize_t (*store)(struct class *class, struct class_attribute *attr,
414                         const char *buf, size_t count);
415         const void *(*namespace)(struct class *class,
416                                  const struct class_attribute *attr);
417 };
418
419 #define CLASS_ATTR(_name, _mode, _show, _store) \
420         struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
421 #define CLASS_ATTR_RW(_name) \
422         struct class_attribute class_attr_##_name = __ATTR_RW(_name)
423 #define CLASS_ATTR_RO(_name) \
424         struct class_attribute class_attr_##_name = __ATTR_RO(_name)
425
426 extern int __must_check class_create_file(struct class *class,
427                                           const struct class_attribute *attr);
428 extern void class_remove_file(struct class *class,
429                               const struct class_attribute *attr);
430
431 /* Simple class attribute that is just a static string */
432 struct class_attribute_string {
433         struct class_attribute attr;
434         char *str;
435 };
436
437 /* Currently read-only only */
438 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
439         { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
440 #define CLASS_ATTR_STRING(_name, _mode, _str) \
441         struct class_attribute_string class_attr_##_name = \
442                 _CLASS_ATTR_STRING(_name, _mode, _str)
443
444 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
445                         char *buf);
446
447 struct class_interface {
448         struct list_head        node;
449         struct class            *class;
450
451         int (*add_dev)          (struct device *, struct class_interface *);
452         void (*remove_dev)      (struct device *, struct class_interface *);
453 };
454
455 extern int __must_check class_interface_register(struct class_interface *);
456 extern void class_interface_unregister(struct class_interface *);
457
458 extern struct class * __must_check __class_create(struct module *owner,
459                                                   const char *name,
460                                                   struct lock_class_key *key);
461 extern void class_destroy(struct class *cls);
462
463 /* This is a #define to keep the compiler from merging different
464  * instances of the __key variable */
465 #define class_create(owner, name)               \
466 ({                                              \
467         static struct lock_class_key __key;     \
468         __class_create(owner, name, &__key);    \
469 })
470
471 /*
472  * The type of device, "struct device" is embedded in. A class
473  * or bus can contain devices of different types
474  * like "partitions" and "disks", "mouse" and "event".
475  * This identifies the device type and carries type-specific
476  * information, equivalent to the kobj_type of a kobject.
477  * If "name" is specified, the uevent will contain it in
478  * the DEVTYPE variable.
479  */
480 struct device_type {
481         const char *name;
482         const struct attribute_group **groups;
483         int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
484         char *(*devnode)(struct device *dev, umode_t *mode,
485                          kuid_t *uid, kgid_t *gid);
486         void (*release)(struct device *dev);
487
488         const struct dev_pm_ops *pm;
489 };
490
491 /* interface for exporting device attributes */
492 struct device_attribute {
493         struct attribute        attr;
494         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
495                         char *buf);
496         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
497                          const char *buf, size_t count);
498 };
499
500 struct dev_ext_attribute {
501         struct device_attribute attr;
502         void *var;
503 };
504
505 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
506                           char *buf);
507 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
508                            const char *buf, size_t count);
509 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
510                         char *buf);
511 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
512                          const char *buf, size_t count);
513 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
514                         char *buf);
515 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
516                          const char *buf, size_t count);
517
518 #define DEVICE_ATTR(_name, _mode, _show, _store) \
519         struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
520 #define DEVICE_ATTR_RW(_name) \
521         struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
522 #define DEVICE_ATTR_RO(_name) \
523         struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
524 #define DEVICE_ATTR_WO(_name) \
525         struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
526 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
527         struct dev_ext_attribute dev_attr_##_name = \
528                 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
529 #define DEVICE_INT_ATTR(_name, _mode, _var) \
530         struct dev_ext_attribute dev_attr_##_name = \
531                 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
532 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
533         struct dev_ext_attribute dev_attr_##_name = \
534                 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
535 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
536         struct device_attribute dev_attr_##_name =              \
537                 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
538
539 extern int device_create_file(struct device *device,
540                               const struct device_attribute *entry);
541 extern void device_remove_file(struct device *dev,
542                                const struct device_attribute *attr);
543 extern int __must_check device_create_bin_file(struct device *dev,
544                                         const struct bin_attribute *attr);
545 extern void device_remove_bin_file(struct device *dev,
546                                    const struct bin_attribute *attr);
547 extern int device_schedule_callback_owner(struct device *dev,
548                 void (*func)(struct device *dev), struct module *owner);
549
550 /* This is a macro to avoid include problems with THIS_MODULE */
551 #define device_schedule_callback(dev, func)                     \
552         device_schedule_callback_owner(dev, func, THIS_MODULE)
553
554 /* device resource management */
555 typedef void (*dr_release_t)(struct device *dev, void *res);
556 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
557
558 #ifdef CONFIG_DEBUG_DEVRES
559 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
560                              const char *name);
561 #define devres_alloc(release, size, gfp) \
562         __devres_alloc(release, size, gfp, #release)
563 #else
564 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
565 #endif
566 extern void devres_for_each_res(struct device *dev, dr_release_t release,
567                                 dr_match_t match, void *match_data,
568                                 void (*fn)(struct device *, void *, void *),
569                                 void *data);
570 extern void devres_free(void *res);
571 extern void devres_add(struct device *dev, void *res);
572 extern void *devres_find(struct device *dev, dr_release_t release,
573                          dr_match_t match, void *match_data);
574 extern void *devres_get(struct device *dev, void *new_res,
575                         dr_match_t match, void *match_data);
576 extern void *devres_remove(struct device *dev, dr_release_t release,
577                            dr_match_t match, void *match_data);
578 extern int devres_destroy(struct device *dev, dr_release_t release,
579                           dr_match_t match, void *match_data);
580 extern int devres_release(struct device *dev, dr_release_t release,
581                           dr_match_t match, void *match_data);
582
583 /* devres group */
584 extern void * __must_check devres_open_group(struct device *dev, void *id,
585                                              gfp_t gfp);
586 extern void devres_close_group(struct device *dev, void *id);
587 extern void devres_remove_group(struct device *dev, void *id);
588 extern int devres_release_group(struct device *dev, void *id);
589
590 /* managed devm_k.alloc/kfree for device drivers */
591 extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
592 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
593 {
594         return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
595 }
596 static inline void *devm_kmalloc_array(struct device *dev,
597                                        size_t n, size_t size, gfp_t flags)
598 {
599         if (size != 0 && n > SIZE_MAX / size)
600                 return NULL;
601         return devm_kmalloc(dev, n * size, flags);
602 }
603 static inline void *devm_kcalloc(struct device *dev,
604                                  size_t n, size_t size, gfp_t flags)
605 {
606         return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
607 }
608 extern void devm_kfree(struct device *dev, void *p);
609
610 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
611 void __iomem *devm_request_and_ioremap(struct device *dev,
612                         struct resource *res);
613
614 /* allows to add/remove a custom action to devres stack */
615 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
616 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
617
618 struct device_dma_parameters {
619         /*
620          * a low level driver may set these to teach IOMMU code about
621          * sg limitations.
622          */
623         unsigned int max_segment_size;
624         unsigned long segment_boundary_mask;
625 };
626
627 struct acpi_dev_node {
628 #ifdef CONFIG_ACPI
629         void    *handle;
630 #endif
631 };
632
633 /**
634  * struct device - The basic device structure
635  * @parent:     The device's "parent" device, the device to which it is attached.
636  *              In most cases, a parent device is some sort of bus or host
637  *              controller. If parent is NULL, the device, is a top-level device,
638  *              which is not usually what you want.
639  * @p:          Holds the private data of the driver core portions of the device.
640  *              See the comment of the struct device_private for detail.
641  * @kobj:       A top-level, abstract class from which other classes are derived.
642  * @init_name:  Initial name of the device.
643  * @type:       The type of device.
644  *              This identifies the device type and carries type-specific
645  *              information.
646  * @mutex:      Mutex to synchronize calls to its driver.
647  * @bus:        Type of bus device is on.
648  * @driver:     Which driver has allocated this
649  * @platform_data: Platform data specific to the device.
650  *              Example: For devices on custom boards, as typical of embedded
651  *              and SOC based hardware, Linux often uses platform_data to point
652  *              to board-specific structures describing devices and how they
653  *              are wired.  That can include what ports are available, chip
654  *              variants, which GPIO pins act in what additional roles, and so
655  *              on.  This shrinks the "Board Support Packages" (BSPs) and
656  *              minimizes board-specific #ifdefs in drivers.
657  * @power:      For device power management.
658  *              See Documentation/power/devices.txt for details.
659  * @pm_domain:  Provide callbacks that are executed during system suspend,
660  *              hibernation, system resume and during runtime PM transitions
661  *              along with subsystem-level and driver-level callbacks.
662  * @pins:       For device pin management.
663  *              See Documentation/pinctrl.txt for details.
664  * @numa_node:  NUMA node this device is close to.
665  * @dma_mask:   Dma mask (if dma'ble device).
666  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
667  *              hardware supports 64-bit addresses for consistent allocations
668  *              such descriptors.
669  * @dma_parms:  A low level driver may set these to teach IOMMU code about
670  *              segment limitations.
671  * @dma_pools:  Dma pools (if dma'ble device).
672  * @dma_mem:    Internal for coherent mem override.
673  * @archdata:   For arch-specific additions.
674  * @of_node:    Associated device tree node.
675  * @acpi_node:  Associated ACPI device node.
676  * @devt:       For creating the sysfs "dev".
677  * @id:         device instance
678  * @devres_lock: Spinlock to protect the resource of the device.
679  * @devres_head: The resources list of the device.
680  * @knode_class: The node used to add the device to the class list.
681  * @class:      The class of the device.
682  * @groups:     Optional attribute groups.
683  * @release:    Callback to free the device after all references have
684  *              gone away. This should be set by the allocator of the
685  *              device (i.e. the bus driver that discovered the device).
686  *
687  * At the lowest level, every device in a Linux system is represented by an
688  * instance of struct device. The device structure contains the information
689  * that the device model core needs to model the system. Most subsystems,
690  * however, track additional information about the devices they host. As a
691  * result, it is rare for devices to be represented by bare device structures;
692  * instead, that structure, like kobject structures, is usually embedded within
693  * a higher-level representation of the device.
694  */
695 struct device {
696         struct device           *parent;
697
698         struct device_private   *p;
699
700         struct kobject kobj;
701         const char              *init_name; /* initial name of the device */
702         const struct device_type *type;
703
704         struct mutex            mutex;  /* mutex to synchronize calls to
705                                          * its driver.
706                                          */
707
708         struct bus_type *bus;           /* type of bus device is on */
709         struct device_driver *driver;   /* which driver has allocated this
710                                            device */
711         void            *platform_data; /* Platform specific data, device
712                                            core doesn't touch it */
713         struct dev_pm_info      power;
714         struct dev_pm_domain    *pm_domain;
715
716 #ifdef CONFIG_PINCTRL
717         struct dev_pin_info     *pins;
718 #endif
719
720 #ifdef CONFIG_NUMA
721         int             numa_node;      /* NUMA node this device is close to */
722 #endif
723         u64             *dma_mask;      /* dma mask (if dma'able device) */
724         u64             coherent_dma_mask;/* Like dma_mask, but for
725                                              alloc_coherent mappings as
726                                              not all hardware supports
727                                              64 bit addresses for consistent
728                                              allocations such descriptors. */
729
730         struct device_dma_parameters *dma_parms;
731
732         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
733
734         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
735                                              override */
736 #ifdef CONFIG_DMA_CMA
737         struct cma *cma_area;           /* contiguous memory area for dma
738                                            allocations */
739 #endif
740         /* arch specific additions */
741         struct dev_archdata     archdata;
742
743         struct device_node      *of_node; /* associated device tree node */
744         struct acpi_dev_node    acpi_node; /* associated ACPI device node */
745
746         dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
747         u32                     id;     /* device instance */
748
749         spinlock_t              devres_lock;
750         struct list_head        devres_head;
751
752         struct klist_node       knode_class;
753         struct class            *class;
754         const struct attribute_group **groups;  /* optional groups */
755
756         void    (*release)(struct device *dev);
757         struct iommu_group      *iommu_group;
758 };
759
760 static inline struct device *kobj_to_dev(struct kobject *kobj)
761 {
762         return container_of(kobj, struct device, kobj);
763 }
764
765 #ifdef CONFIG_ACPI
766 #define ACPI_HANDLE(dev)        ((dev)->acpi_node.handle)
767 #define ACPI_HANDLE_SET(dev, _handle_)  (dev)->acpi_node.handle = (_handle_)
768 #else
769 #define ACPI_HANDLE(dev)        (NULL)
770 #define ACPI_HANDLE_SET(dev, _handle_)  do { } while (0)
771 #endif
772
773 /* Get the wakeup routines, which depend on struct device */
774 #include <linux/pm_wakeup.h>
775
776 static inline const char *dev_name(const struct device *dev)
777 {
778         /* Use the init name until the kobject becomes available */
779         if (dev->init_name)
780                 return dev->init_name;
781
782         return kobject_name(&dev->kobj);
783 }
784
785 extern __printf(2, 3)
786 int dev_set_name(struct device *dev, const char *name, ...);
787
788 #ifdef CONFIG_NUMA
789 static inline int dev_to_node(struct device *dev)
790 {
791         return dev->numa_node;
792 }
793 static inline void set_dev_node(struct device *dev, int node)
794 {
795         dev->numa_node = node;
796 }
797 #else
798 static inline int dev_to_node(struct device *dev)
799 {
800         return -1;
801 }
802 static inline void set_dev_node(struct device *dev, int node)
803 {
804 }
805 #endif
806
807 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
808 {
809         return dev ? dev->power.subsys_data : NULL;
810 }
811
812 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
813 {
814         return dev->kobj.uevent_suppress;
815 }
816
817 static inline void dev_set_uevent_suppress(struct device *dev, int val)
818 {
819         dev->kobj.uevent_suppress = val;
820 }
821
822 static inline int device_is_registered(struct device *dev)
823 {
824         return dev->kobj.state_in_sysfs;
825 }
826
827 static inline void device_enable_async_suspend(struct device *dev)
828 {
829         if (!dev->power.is_prepared)
830                 dev->power.async_suspend = true;
831 }
832
833 static inline void device_disable_async_suspend(struct device *dev)
834 {
835         if (!dev->power.is_prepared)
836                 dev->power.async_suspend = false;
837 }
838
839 static inline bool device_async_suspend_enabled(struct device *dev)
840 {
841         return !!dev->power.async_suspend;
842 }
843
844 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
845 {
846         dev->power.ignore_children = enable;
847 }
848
849 static inline void dev_pm_syscore_device(struct device *dev, bool val)
850 {
851 #ifdef CONFIG_PM_SLEEP
852         dev->power.syscore = val;
853 #endif
854 }
855
856 static inline void device_lock(struct device *dev)
857 {
858         mutex_lock(&dev->mutex);
859 }
860
861 static inline int device_trylock(struct device *dev)
862 {
863         return mutex_trylock(&dev->mutex);
864 }
865
866 static inline void device_unlock(struct device *dev)
867 {
868         mutex_unlock(&dev->mutex);
869 }
870
871 void driver_init(void);
872
873 /*
874  * High level routines for use by the bus drivers
875  */
876 extern int __must_check device_register(struct device *dev);
877 extern void device_unregister(struct device *dev);
878 extern void device_initialize(struct device *dev);
879 extern int __must_check device_add(struct device *dev);
880 extern void device_del(struct device *dev);
881 extern int device_for_each_child(struct device *dev, void *data,
882                      int (*fn)(struct device *dev, void *data));
883 extern struct device *device_find_child(struct device *dev, void *data,
884                                 int (*match)(struct device *dev, void *data));
885 extern int device_rename(struct device *dev, const char *new_name);
886 extern int device_move(struct device *dev, struct device *new_parent,
887                        enum dpm_order dpm_order);
888 extern const char *device_get_devnode(struct device *dev,
889                                       umode_t *mode, kuid_t *uid, kgid_t *gid,
890                                       const char **tmp);
891 extern void *dev_get_drvdata(const struct device *dev);
892 extern int dev_set_drvdata(struct device *dev, void *data);
893
894 /*
895  * Root device objects for grouping under /sys/devices
896  */
897 extern struct device *__root_device_register(const char *name,
898                                              struct module *owner);
899
900 /*
901  * This is a macro to avoid include problems with THIS_MODULE,
902  * just as per what is done for device_schedule_callback() above.
903  */
904 #define root_device_register(name) \
905         __root_device_register(name, THIS_MODULE)
906
907 extern void root_device_unregister(struct device *root);
908
909 static inline void *dev_get_platdata(const struct device *dev)
910 {
911         return dev->platform_data;
912 }
913
914 /*
915  * Manual binding of a device to driver. See drivers/base/bus.c
916  * for information on use.
917  */
918 extern int __must_check device_bind_driver(struct device *dev);
919 extern void device_release_driver(struct device *dev);
920 extern int  __must_check device_attach(struct device *dev);
921 extern int __must_check driver_attach(struct device_driver *drv);
922 extern int __must_check device_reprobe(struct device *dev);
923
924 /*
925  * Easy functions for dynamically creating devices on the fly
926  */
927 extern struct device *device_create_vargs(struct class *cls,
928                                           struct device *parent,
929                                           dev_t devt,
930                                           void *drvdata,
931                                           const char *fmt,
932                                           va_list vargs);
933 extern __printf(5, 6)
934 struct device *device_create(struct class *cls, struct device *parent,
935                              dev_t devt, void *drvdata,
936                              const char *fmt, ...);
937 extern void device_destroy(struct class *cls, dev_t devt);
938
939 /*
940  * Platform "fixup" functions - allow the platform to have their say
941  * about devices and actions that the general device layer doesn't
942  * know about.
943  */
944 /* Notify platform of device discovery */
945 extern int (*platform_notify)(struct device *dev);
946
947 extern int (*platform_notify_remove)(struct device *dev);
948
949
950 /*
951  * get_device - atomically increment the reference count for the device.
952  *
953  */
954 extern struct device *get_device(struct device *dev);
955 extern void put_device(struct device *dev);
956
957 #ifdef CONFIG_DEVTMPFS
958 extern int devtmpfs_create_node(struct device *dev);
959 extern int devtmpfs_delete_node(struct device *dev);
960 extern int devtmpfs_mount(const char *mntdir);
961 #else
962 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
963 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
964 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
965 #endif
966
967 /* drivers/base/power/shutdown.c */
968 extern void device_shutdown(void);
969
970 /* debugging and troubleshooting/diagnostic helpers. */
971 extern const char *dev_driver_string(const struct device *dev);
972
973
974 #ifdef CONFIG_PRINTK
975
976 extern __printf(3, 0)
977 int dev_vprintk_emit(int level, const struct device *dev,
978                      const char *fmt, va_list args);
979 extern __printf(3, 4)
980 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
981
982 extern __printf(3, 4)
983 int dev_printk(const char *level, const struct device *dev,
984                const char *fmt, ...);
985 extern __printf(2, 3)
986 int dev_emerg(const struct device *dev, const char *fmt, ...);
987 extern __printf(2, 3)
988 int dev_alert(const struct device *dev, const char *fmt, ...);
989 extern __printf(2, 3)
990 int dev_crit(const struct device *dev, const char *fmt, ...);
991 extern __printf(2, 3)
992 int dev_err(const struct device *dev, const char *fmt, ...);
993 extern __printf(2, 3)
994 int dev_warn(const struct device *dev, const char *fmt, ...);
995 extern __printf(2, 3)
996 int dev_notice(const struct device *dev, const char *fmt, ...);
997 extern __printf(2, 3)
998 int _dev_info(const struct device *dev, const char *fmt, ...);
999
1000 #else
1001
1002 static inline __printf(3, 0)
1003 int dev_vprintk_emit(int level, const struct device *dev,
1004                      const char *fmt, va_list args)
1005 { return 0; }
1006 static inline __printf(3, 4)
1007 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1008 { return 0; }
1009
1010 static inline int __dev_printk(const char *level, const struct device *dev,
1011                                struct va_format *vaf)
1012 { return 0; }
1013 static inline __printf(3, 4)
1014 int dev_printk(const char *level, const struct device *dev,
1015                const char *fmt, ...)
1016 { return 0; }
1017
1018 static inline __printf(2, 3)
1019 int dev_emerg(const struct device *dev, const char *fmt, ...)
1020 { return 0; }
1021 static inline __printf(2, 3)
1022 int dev_crit(const struct device *dev, const char *fmt, ...)
1023 { return 0; }
1024 static inline __printf(2, 3)
1025 int dev_alert(const struct device *dev, const char *fmt, ...)
1026 { return 0; }
1027 static inline __printf(2, 3)
1028 int dev_err(const struct device *dev, const char *fmt, ...)
1029 { return 0; }
1030 static inline __printf(2, 3)
1031 int dev_warn(const struct device *dev, const char *fmt, ...)
1032 { return 0; }
1033 static inline __printf(2, 3)
1034 int dev_notice(const struct device *dev, const char *fmt, ...)
1035 { return 0; }
1036 static inline __printf(2, 3)
1037 int _dev_info(const struct device *dev, const char *fmt, ...)
1038 { return 0; }
1039
1040 #endif
1041
1042 /*
1043  * Stupid hackaround for existing uses of non-printk uses dev_info
1044  *
1045  * Note that the definition of dev_info below is actually _dev_info
1046  * and a macro is used to avoid redefining dev_info
1047  */
1048
1049 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
1050
1051 #if defined(CONFIG_DYNAMIC_DEBUG)
1052 #define dev_dbg(dev, format, ...)                    \
1053 do {                                                 \
1054         dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
1055 } while (0)
1056 #elif defined(DEBUG)
1057 #define dev_dbg(dev, format, arg...)            \
1058         dev_printk(KERN_DEBUG, dev, format, ##arg)
1059 #else
1060 #define dev_dbg(dev, format, arg...)                            \
1061 ({                                                              \
1062         if (0)                                                  \
1063                 dev_printk(KERN_DEBUG, dev, format, ##arg);     \
1064         0;                                                      \
1065 })
1066 #endif
1067
1068 #define dev_level_ratelimited(dev_level, dev, fmt, ...)                 \
1069 do {                                                                    \
1070         static DEFINE_RATELIMIT_STATE(_rs,                              \
1071                                       DEFAULT_RATELIMIT_INTERVAL,       \
1072                                       DEFAULT_RATELIMIT_BURST);         \
1073         if (__ratelimit(&_rs))                                          \
1074                 dev_level(dev, fmt, ##__VA_ARGS__);                     \
1075 } while (0)
1076
1077 #define dev_emerg_ratelimited(dev, fmt, ...)                            \
1078         dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1079 #define dev_alert_ratelimited(dev, fmt, ...)                            \
1080         dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1081 #define dev_crit_ratelimited(dev, fmt, ...)                             \
1082         dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1083 #define dev_err_ratelimited(dev, fmt, ...)                              \
1084         dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1085 #define dev_warn_ratelimited(dev, fmt, ...)                             \
1086         dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1087 #define dev_notice_ratelimited(dev, fmt, ...)                           \
1088         dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1089 #define dev_info_ratelimited(dev, fmt, ...)                             \
1090         dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1091 #if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1092 #define dev_dbg_ratelimited(dev, fmt, ...)                              \
1093 do {                                                                    \
1094         static DEFINE_RATELIMIT_STATE(_rs,                              \
1095                                       DEFAULT_RATELIMIT_INTERVAL,       \
1096                                       DEFAULT_RATELIMIT_BURST);         \
1097         DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);                 \
1098         if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) &&        \
1099             __ratelimit(&_rs))                                          \
1100                 __dynamic_pr_debug(&descriptor, pr_fmt(fmt),            \
1101                                    ##__VA_ARGS__);                      \
1102 } while (0)
1103 #else
1104 #define dev_dbg_ratelimited(dev, fmt, ...)                      \
1105         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
1106 #endif
1107
1108 #ifdef VERBOSE_DEBUG
1109 #define dev_vdbg        dev_dbg
1110 #else
1111 #define dev_vdbg(dev, format, arg...)                           \
1112 ({                                                              \
1113         if (0)                                                  \
1114                 dev_printk(KERN_DEBUG, dev, format, ##arg);     \
1115         0;                                                      \
1116 })
1117 #endif
1118
1119 /*
1120  * dev_WARN*() acts like dev_printk(), but with the key difference
1121  * of using a WARN/WARN_ON to get the message out, including the
1122  * file/line information and a backtrace.
1123  */
1124 #define dev_WARN(dev, format, arg...) \
1125         WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
1126
1127 #define dev_WARN_ONCE(dev, condition, format, arg...) \
1128         WARN_ONCE(condition, "Device %s\n" format, \
1129                         dev_driver_string(dev), ## arg)
1130
1131 /* Create alias, so I can be autoloaded. */
1132 #define MODULE_ALIAS_CHARDEV(major,minor) \
1133         MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1134 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1135         MODULE_ALIAS("char-major-" __stringify(major) "-*")
1136
1137 #ifdef CONFIG_SYSFS_DEPRECATED
1138 extern long sysfs_deprecated;
1139 #else
1140 #define sysfs_deprecated 0
1141 #endif
1142
1143 /**
1144  * module_driver() - Helper macro for drivers that don't do anything
1145  * special in module init/exit. This eliminates a lot of boilerplate.
1146  * Each module may only use this macro once, and calling it replaces
1147  * module_init() and module_exit().
1148  *
1149  * @__driver: driver name
1150  * @__register: register function for this driver type
1151  * @__unregister: unregister function for this driver type
1152  * @...: Additional arguments to be passed to __register and __unregister.
1153  *
1154  * Use this macro to construct bus specific macros for registering
1155  * drivers, and do not use it on its own.
1156  */
1157 #define module_driver(__driver, __register, __unregister, ...) \
1158 static int __init __driver##_init(void) \
1159 { \
1160         return __register(&(__driver) , ##__VA_ARGS__); \
1161 } \
1162 module_init(__driver##_init); \
1163 static void __exit __driver##_exit(void) \
1164 { \
1165         __unregister(&(__driver) , ##__VA_ARGS__); \
1166 } \
1167 module_exit(__driver##_exit);
1168
1169 #endif /* _DEVICE_H_ */