Merge tag 'lsk-v3.10-android-15.01'
[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 void __iomem *devm_ioremap_exec_resource(struct device *dev,
615                         struct resource *res);
616 void __iomem *devm_request_and_ioremap_exec(struct device *dev,
617                         struct resource *res);
618
619 /* allows to add/remove a custom action to devres stack */
620 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
621 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
622
623 struct device_dma_parameters {
624         /*
625          * a low level driver may set these to teach IOMMU code about
626          * sg limitations.
627          */
628         unsigned int max_segment_size;
629         unsigned long segment_boundary_mask;
630 };
631
632 struct acpi_dev_node {
633 #ifdef CONFIG_ACPI
634         void    *handle;
635 #endif
636 };
637
638 /**
639  * struct device - The basic device structure
640  * @parent:     The device's "parent" device, the device to which it is attached.
641  *              In most cases, a parent device is some sort of bus or host
642  *              controller. If parent is NULL, the device, is a top-level device,
643  *              which is not usually what you want.
644  * @p:          Holds the private data of the driver core portions of the device.
645  *              See the comment of the struct device_private for detail.
646  * @kobj:       A top-level, abstract class from which other classes are derived.
647  * @init_name:  Initial name of the device.
648  * @type:       The type of device.
649  *              This identifies the device type and carries type-specific
650  *              information.
651  * @mutex:      Mutex to synchronize calls to its driver.
652  * @bus:        Type of bus device is on.
653  * @driver:     Which driver has allocated this
654  * @platform_data: Platform data specific to the device.
655  *              Example: For devices on custom boards, as typical of embedded
656  *              and SOC based hardware, Linux often uses platform_data to point
657  *              to board-specific structures describing devices and how they
658  *              are wired.  That can include what ports are available, chip
659  *              variants, which GPIO pins act in what additional roles, and so
660  *              on.  This shrinks the "Board Support Packages" (BSPs) and
661  *              minimizes board-specific #ifdefs in drivers.
662  * @power:      For device power management.
663  *              See Documentation/power/devices.txt for details.
664  * @pm_domain:  Provide callbacks that are executed during system suspend,
665  *              hibernation, system resume and during runtime PM transitions
666  *              along with subsystem-level and driver-level callbacks.
667  * @pins:       For device pin management.
668  *              See Documentation/pinctrl.txt for details.
669  * @numa_node:  NUMA node this device is close to.
670  * @dma_mask:   Dma mask (if dma'ble device).
671  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
672  *              hardware supports 64-bit addresses for consistent allocations
673  *              such descriptors.
674  * @dma_parms:  A low level driver may set these to teach IOMMU code about
675  *              segment limitations.
676  * @dma_pools:  Dma pools (if dma'ble device).
677  * @dma_mem:    Internal for coherent mem override.
678  * @archdata:   For arch-specific additions.
679  * @of_node:    Associated device tree node.
680  * @acpi_node:  Associated ACPI device node.
681  * @devt:       For creating the sysfs "dev".
682  * @id:         device instance
683  * @devres_lock: Spinlock to protect the resource of the device.
684  * @devres_head: The resources list of the device.
685  * @knode_class: The node used to add the device to the class list.
686  * @class:      The class of the device.
687  * @groups:     Optional attribute groups.
688  * @release:    Callback to free the device after all references have
689  *              gone away. This should be set by the allocator of the
690  *              device (i.e. the bus driver that discovered the device).
691  *
692  * At the lowest level, every device in a Linux system is represented by an
693  * instance of struct device. The device structure contains the information
694  * that the device model core needs to model the system. Most subsystems,
695  * however, track additional information about the devices they host. As a
696  * result, it is rare for devices to be represented by bare device structures;
697  * instead, that structure, like kobject structures, is usually embedded within
698  * a higher-level representation of the device.
699  */
700 struct device {
701         struct device           *parent;
702
703         struct device_private   *p;
704
705         struct kobject kobj;
706         const char              *init_name; /* initial name of the device */
707         const struct device_type *type;
708
709         struct mutex            mutex;  /* mutex to synchronize calls to
710                                          * its driver.
711                                          */
712
713         struct bus_type *bus;           /* type of bus device is on */
714         struct device_driver *driver;   /* which driver has allocated this
715                                            device */
716         void            *platform_data; /* Platform specific data, device
717                                            core doesn't touch it */
718         struct dev_pm_info      power;
719         struct dev_pm_domain    *pm_domain;
720
721 #ifdef CONFIG_PINCTRL
722         struct dev_pin_info     *pins;
723 #endif
724
725 #ifdef CONFIG_NUMA
726         int             numa_node;      /* NUMA node this device is close to */
727 #endif
728         u64             *dma_mask;      /* dma mask (if dma'able device) */
729         u64             coherent_dma_mask;/* Like dma_mask, but for
730                                              alloc_coherent mappings as
731                                              not all hardware supports
732                                              64 bit addresses for consistent
733                                              allocations such descriptors. */
734
735         struct device_dma_parameters *dma_parms;
736
737         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
738
739         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
740                                              override */
741 #ifdef CONFIG_DMA_CMA
742         struct cma *cma_area;           /* contiguous memory area for dma
743                                            allocations */
744 #endif
745         /* arch specific additions */
746         struct dev_archdata     archdata;
747
748         struct device_node      *of_node; /* associated device tree node */
749         struct acpi_dev_node    acpi_node; /* associated ACPI device node */
750
751         dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
752         u32                     id;     /* device instance */
753
754         spinlock_t              devres_lock;
755         struct list_head        devres_head;
756
757         struct klist_node       knode_class;
758         struct class            *class;
759         const struct attribute_group **groups;  /* optional groups */
760
761         void    (*release)(struct device *dev);
762         struct iommu_group      *iommu_group;
763 };
764
765 static inline struct device *kobj_to_dev(struct kobject *kobj)
766 {
767         return container_of(kobj, struct device, kobj);
768 }
769
770 #ifdef CONFIG_ACPI
771 #define ACPI_HANDLE(dev)        ((dev)->acpi_node.handle)
772 #define ACPI_HANDLE_SET(dev, _handle_)  (dev)->acpi_node.handle = (_handle_)
773 #else
774 #define ACPI_HANDLE(dev)        (NULL)
775 #define ACPI_HANDLE_SET(dev, _handle_)  do { } while (0)
776 #endif
777
778 /* Get the wakeup routines, which depend on struct device */
779 #include <linux/pm_wakeup.h>
780
781 static inline const char *dev_name(const struct device *dev)
782 {
783         /* Use the init name until the kobject becomes available */
784         if (dev->init_name)
785                 return dev->init_name;
786
787         return kobject_name(&dev->kobj);
788 }
789
790 extern __printf(2, 3)
791 int dev_set_name(struct device *dev, const char *name, ...);
792
793 #ifdef CONFIG_NUMA
794 static inline int dev_to_node(struct device *dev)
795 {
796         return dev->numa_node;
797 }
798 static inline void set_dev_node(struct device *dev, int node)
799 {
800         dev->numa_node = node;
801 }
802 #else
803 static inline int dev_to_node(struct device *dev)
804 {
805         return -1;
806 }
807 static inline void set_dev_node(struct device *dev, int node)
808 {
809 }
810 #endif
811
812 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
813 {
814         return dev ? dev->power.subsys_data : NULL;
815 }
816
817 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
818 {
819         return dev->kobj.uevent_suppress;
820 }
821
822 static inline void dev_set_uevent_suppress(struct device *dev, int val)
823 {
824         dev->kobj.uevent_suppress = val;
825 }
826
827 static inline int device_is_registered(struct device *dev)
828 {
829         return dev->kobj.state_in_sysfs;
830 }
831
832 static inline void device_enable_async_suspend(struct device *dev)
833 {
834         if (!dev->power.is_prepared)
835                 dev->power.async_suspend = true;
836 }
837
838 static inline void device_disable_async_suspend(struct device *dev)
839 {
840         if (!dev->power.is_prepared)
841                 dev->power.async_suspend = false;
842 }
843
844 static inline bool device_async_suspend_enabled(struct device *dev)
845 {
846         return !!dev->power.async_suspend;
847 }
848
849 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
850 {
851         dev->power.ignore_children = enable;
852 }
853
854 static inline void dev_pm_syscore_device(struct device *dev, bool val)
855 {
856 #ifdef CONFIG_PM_SLEEP
857         dev->power.syscore = val;
858 #endif
859 }
860
861 static inline void device_lock(struct device *dev)
862 {
863         mutex_lock(&dev->mutex);
864 }
865
866 static inline int device_trylock(struct device *dev)
867 {
868         return mutex_trylock(&dev->mutex);
869 }
870
871 static inline void device_unlock(struct device *dev)
872 {
873         mutex_unlock(&dev->mutex);
874 }
875
876 void driver_init(void);
877
878 /*
879  * High level routines for use by the bus drivers
880  */
881 extern int __must_check device_register(struct device *dev);
882 extern void device_unregister(struct device *dev);
883 extern void device_initialize(struct device *dev);
884 extern int __must_check device_add(struct device *dev);
885 extern void device_del(struct device *dev);
886 extern int device_for_each_child(struct device *dev, void *data,
887                      int (*fn)(struct device *dev, void *data));
888 extern struct device *device_find_child(struct device *dev, void *data,
889                                 int (*match)(struct device *dev, void *data));
890 extern int device_rename(struct device *dev, const char *new_name);
891 extern int device_move(struct device *dev, struct device *new_parent,
892                        enum dpm_order dpm_order);
893 extern const char *device_get_devnode(struct device *dev,
894                                       umode_t *mode, kuid_t *uid, kgid_t *gid,
895                                       const char **tmp);
896 extern void *dev_get_drvdata(const struct device *dev);
897 extern int dev_set_drvdata(struct device *dev, void *data);
898
899 /*
900  * Root device objects for grouping under /sys/devices
901  */
902 extern struct device *__root_device_register(const char *name,
903                                              struct module *owner);
904
905 /*
906  * This is a macro to avoid include problems with THIS_MODULE,
907  * just as per what is done for device_schedule_callback() above.
908  */
909 #define root_device_register(name) \
910         __root_device_register(name, THIS_MODULE)
911
912 extern void root_device_unregister(struct device *root);
913
914 static inline void *dev_get_platdata(const struct device *dev)
915 {
916         return dev->platform_data;
917 }
918
919 /*
920  * Manual binding of a device to driver. See drivers/base/bus.c
921  * for information on use.
922  */
923 extern int __must_check device_bind_driver(struct device *dev);
924 extern void device_release_driver(struct device *dev);
925 extern int  __must_check device_attach(struct device *dev);
926 extern int __must_check driver_attach(struct device_driver *drv);
927 extern int __must_check device_reprobe(struct device *dev);
928
929 /*
930  * Easy functions for dynamically creating devices on the fly
931  */
932 extern struct device *device_create_vargs(struct class *cls,
933                                           struct device *parent,
934                                           dev_t devt,
935                                           void *drvdata,
936                                           const char *fmt,
937                                           va_list vargs);
938 extern __printf(5, 6)
939 struct device *device_create(struct class *cls, struct device *parent,
940                              dev_t devt, void *drvdata,
941                              const char *fmt, ...);
942 extern void device_destroy(struct class *cls, dev_t devt);
943
944 /*
945  * Platform "fixup" functions - allow the platform to have their say
946  * about devices and actions that the general device layer doesn't
947  * know about.
948  */
949 /* Notify platform of device discovery */
950 extern int (*platform_notify)(struct device *dev);
951
952 extern int (*platform_notify_remove)(struct device *dev);
953
954
955 /*
956  * get_device - atomically increment the reference count for the device.
957  *
958  */
959 extern struct device *get_device(struct device *dev);
960 extern void put_device(struct device *dev);
961
962 #ifdef CONFIG_DEVTMPFS
963 extern int devtmpfs_create_node(struct device *dev);
964 extern int devtmpfs_delete_node(struct device *dev);
965 extern int devtmpfs_mount(const char *mntdir);
966 #else
967 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
968 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
969 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
970 #endif
971
972 /* drivers/base/power/shutdown.c */
973 extern void device_shutdown(void);
974
975 /* debugging and troubleshooting/diagnostic helpers. */
976 extern const char *dev_driver_string(const struct device *dev);
977
978
979 #ifdef CONFIG_PRINTK
980
981 extern __printf(3, 0)
982 int dev_vprintk_emit(int level, const struct device *dev,
983                      const char *fmt, va_list args);
984 extern __printf(3, 4)
985 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
986
987 extern __printf(3, 4)
988 int dev_printk(const char *level, const struct device *dev,
989                const char *fmt, ...);
990 extern __printf(2, 3)
991 int dev_emerg(const struct device *dev, const char *fmt, ...);
992 extern __printf(2, 3)
993 int dev_alert(const struct device *dev, const char *fmt, ...);
994 extern __printf(2, 3)
995 int dev_crit(const struct device *dev, const char *fmt, ...);
996 extern __printf(2, 3)
997 int dev_err(const struct device *dev, const char *fmt, ...);
998 extern __printf(2, 3)
999 int dev_warn(const struct device *dev, const char *fmt, ...);
1000 extern __printf(2, 3)
1001 int dev_notice(const struct device *dev, const char *fmt, ...);
1002 extern __printf(2, 3)
1003 int _dev_info(const struct device *dev, const char *fmt, ...);
1004
1005 #else
1006
1007 static inline __printf(3, 0)
1008 int dev_vprintk_emit(int level, const struct device *dev,
1009                      const char *fmt, va_list args)
1010 { return 0; }
1011 static inline __printf(3, 4)
1012 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1013 { return 0; }
1014
1015 static inline int __dev_printk(const char *level, const struct device *dev,
1016                                struct va_format *vaf)
1017 { return 0; }
1018 static inline __printf(3, 4)
1019 int dev_printk(const char *level, const struct device *dev,
1020                const char *fmt, ...)
1021 { return 0; }
1022
1023 static inline __printf(2, 3)
1024 int dev_emerg(const struct device *dev, const char *fmt, ...)
1025 { return 0; }
1026 static inline __printf(2, 3)
1027 int dev_crit(const struct device *dev, const char *fmt, ...)
1028 { return 0; }
1029 static inline __printf(2, 3)
1030 int dev_alert(const struct device *dev, const char *fmt, ...)
1031 { return 0; }
1032 static inline __printf(2, 3)
1033 int dev_err(const struct device *dev, const char *fmt, ...)
1034 { return 0; }
1035 static inline __printf(2, 3)
1036 int dev_warn(const struct device *dev, const char *fmt, ...)
1037 { return 0; }
1038 static inline __printf(2, 3)
1039 int dev_notice(const struct device *dev, const char *fmt, ...)
1040 { return 0; }
1041 static inline __printf(2, 3)
1042 int _dev_info(const struct device *dev, const char *fmt, ...)
1043 { return 0; }
1044
1045 #endif
1046
1047 /*
1048  * Stupid hackaround for existing uses of non-printk uses dev_info
1049  *
1050  * Note that the definition of dev_info below is actually _dev_info
1051  * and a macro is used to avoid redefining dev_info
1052  */
1053
1054 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
1055
1056 #if defined(CONFIG_DYNAMIC_DEBUG)
1057 #define dev_dbg(dev, format, ...)                    \
1058 do {                                                 \
1059         dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
1060 } while (0)
1061 #elif defined(DEBUG)
1062 #define dev_dbg(dev, format, arg...)            \
1063         dev_printk(KERN_DEBUG, dev, format, ##arg)
1064 #else
1065 #define dev_dbg(dev, format, arg...)                            \
1066 ({                                                              \
1067         if (0)                                                  \
1068                 dev_printk(KERN_DEBUG, dev, format, ##arg);     \
1069         0;                                                      \
1070 })
1071 #endif
1072
1073 #define dev_level_ratelimited(dev_level, dev, fmt, ...)                 \
1074 do {                                                                    \
1075         static DEFINE_RATELIMIT_STATE(_rs,                              \
1076                                       DEFAULT_RATELIMIT_INTERVAL,       \
1077                                       DEFAULT_RATELIMIT_BURST);         \
1078         if (__ratelimit(&_rs))                                          \
1079                 dev_level(dev, fmt, ##__VA_ARGS__);                     \
1080 } while (0)
1081
1082 #define dev_emerg_ratelimited(dev, fmt, ...)                            \
1083         dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1084 #define dev_alert_ratelimited(dev, fmt, ...)                            \
1085         dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1086 #define dev_crit_ratelimited(dev, fmt, ...)                             \
1087         dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1088 #define dev_err_ratelimited(dev, fmt, ...)                              \
1089         dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1090 #define dev_warn_ratelimited(dev, fmt, ...)                             \
1091         dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1092 #define dev_notice_ratelimited(dev, fmt, ...)                           \
1093         dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1094 #define dev_info_ratelimited(dev, fmt, ...)                             \
1095         dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1096 #if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1097 #define dev_dbg_ratelimited(dev, fmt, ...)                              \
1098 do {                                                                    \
1099         static DEFINE_RATELIMIT_STATE(_rs,                              \
1100                                       DEFAULT_RATELIMIT_INTERVAL,       \
1101                                       DEFAULT_RATELIMIT_BURST);         \
1102         DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);                 \
1103         if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) &&        \
1104             __ratelimit(&_rs))                                          \
1105                 __dynamic_pr_debug(&descriptor, pr_fmt(fmt),            \
1106                                    ##__VA_ARGS__);                      \
1107 } while (0)
1108 #else
1109 #define dev_dbg_ratelimited(dev, fmt, ...)                      \
1110         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
1111 #endif
1112
1113 #ifdef VERBOSE_DEBUG
1114 #define dev_vdbg        dev_dbg
1115 #else
1116 #define dev_vdbg(dev, format, arg...)                           \
1117 ({                                                              \
1118         if (0)                                                  \
1119                 dev_printk(KERN_DEBUG, dev, format, ##arg);     \
1120         0;                                                      \
1121 })
1122 #endif
1123
1124 /*
1125  * dev_WARN*() acts like dev_printk(), but with the key difference
1126  * of using a WARN/WARN_ON to get the message out, including the
1127  * file/line information and a backtrace.
1128  */
1129 #define dev_WARN(dev, format, arg...) \
1130         WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
1131
1132 #define dev_WARN_ONCE(dev, condition, format, arg...) \
1133         WARN_ONCE(condition, "Device %s\n" format, \
1134                         dev_driver_string(dev), ## arg)
1135
1136 /* Create alias, so I can be autoloaded. */
1137 #define MODULE_ALIAS_CHARDEV(major,minor) \
1138         MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1139 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1140         MODULE_ALIAS("char-major-" __stringify(major) "-*")
1141
1142 #ifdef CONFIG_SYSFS_DEPRECATED
1143 extern long sysfs_deprecated;
1144 #else
1145 #define sysfs_deprecated 0
1146 #endif
1147
1148 /**
1149  * module_driver() - Helper macro for drivers that don't do anything
1150  * special in module init/exit. This eliminates a lot of boilerplate.
1151  * Each module may only use this macro once, and calling it replaces
1152  * module_init() and module_exit().
1153  *
1154  * @__driver: driver name
1155  * @__register: register function for this driver type
1156  * @__unregister: unregister function for this driver type
1157  * @...: Additional arguments to be passed to __register and __unregister.
1158  *
1159  * Use this macro to construct bus specific macros for registering
1160  * drivers, and do not use it on its own.
1161  */
1162 #define module_driver(__driver, __register, __unregister, ...) \
1163 static int __init __driver##_init(void) \
1164 { \
1165         return __register(&(__driver) , ##__VA_ARGS__); \
1166 } \
1167 module_init(__driver##_init); \
1168 static void __exit __driver##_exit(void) \
1169 { \
1170         __unregister(&(__driver) , ##__VA_ARGS__); \
1171 } \
1172 module_exit(__driver##_exit);
1173
1174 #endif /* _DEVICE_H_ */