[305] | 1 | #ifndef _LINUX_DEVICE_H
|
---|
| 2 | #define _LINUX_DEVICE_H
|
---|
[679] | 3 |
|
---|
| 4 | #include <linux/types.h>
|
---|
| 5 | #include <linux/kobject.h>
|
---|
[441] | 6 | #include <linux/pm.h>
|
---|
[679] | 7 | #include <linux/sysfs.h>
|
---|
| 8 | #include <linux/lockdep.h>
|
---|
| 9 | #include <linux/overflow.h>
|
---|
[463] | 10 |
|
---|
[679] | 11 | struct device;
|
---|
| 12 | struct device_private;
|
---|
| 13 | struct device_driver;
|
---|
| 14 | struct driver_private;
|
---|
| 15 | struct class;
|
---|
| 16 | struct subsys_private;
|
---|
| 17 | struct bus_type;
|
---|
| 18 | struct device_node;
|
---|
| 19 |
|
---|
| 20 | struct bus_attribute {
|
---|
| 21 | struct attribute attr;
|
---|
| 22 | ssize_t (*show)(struct bus_type *bus, char *buf);
|
---|
| 23 | ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
|
---|
[305] | 24 | };
|
---|
[679] | 25 |
|
---|
| 26 | /*
|
---|
| 27 | * The type of device, "struct device" is embedded in. A class
|
---|
| 28 | * or bus can contain devices of different types
|
---|
| 29 | * like "partitions" and "disks", "mouse" and "event".
|
---|
| 30 | * This identifies the device type and carries type-specific
|
---|
| 31 | * information, equivalent to the kobj_type of a kobject.
|
---|
| 32 | * If "name" is specified, the uevent will contain it in
|
---|
| 33 | * the DEVTYPE variable.
|
---|
| 34 | */
|
---|
| 35 | struct device_type {
|
---|
| 36 | const char *name;
|
---|
| 37 | const struct attribute_group **groups;
|
---|
| 38 | int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
|
---|
| 39 | /* char *(*devnode)(struct device *dev, umode_t *mode,
|
---|
| 40 | kuid_t *uid, kgid_t *gid);*/
|
---|
| 41 | void (*release)(struct device *dev);
|
---|
| 42 |
|
---|
| 43 | const struct dev_pm_ops *pm;
|
---|
| 44 | };
|
---|
| 45 |
|
---|
[772] | 46 | struct device_dma_parameters {
|
---|
| 47 | /*
|
---|
| 48 | * a low level driver may set these to teach IOMMU code about
|
---|
| 49 | * sg limitations.
|
---|
| 50 | */
|
---|
| 51 | unsigned int max_segment_size;
|
---|
| 52 | unsigned int min_align_mask;
|
---|
| 53 | unsigned long segment_boundary_mask;
|
---|
| 54 | };
|
---|
| 55 |
|
---|
[679] | 56 | typedef struct device {
|
---|
| 57 | struct pci_dev *pci; /* for PCI and PCI-SG types */
|
---|
| 58 | struct device * parent;
|
---|
| 59 | struct device_private *p;
|
---|
| 60 | struct bus_type * bus; /* type of bus device is on */
|
---|
| 61 | struct kobject kobj;
|
---|
| 62 | const char *init_name; /* initial name of the device */
|
---|
| 63 | const struct device_type *type;
|
---|
| 64 | #if 0
|
---|
| 65 | char bus_id[BUS_ID_SIZE]; /* position on parent bus */
|
---|
[305] | 66 | #endif
|
---|
[679] | 67 | dev_t devt; /* dev_t, creates the sysfs "dev" */
|
---|
| 68 | void (*release)(struct device * dev);
|
---|
| 69 | unsigned int flags; /* GFP_XXX for continous and ISA types */
|
---|
| 70 | struct semaphore mutex; /* mutex to synchronize calls to
|
---|
| 71 | * its driver.
|
---|
| 72 | */
|
---|
| 73 | #ifdef CONFIG_SBUS
|
---|
| 74 | struct sbus_dev *sbus; /* for SBUS type */
|
---|
| 75 | #endif
|
---|
| 76 | void *private_data;
|
---|
| 77 | void *platform_data;
|
---|
| 78 | struct dev_pm_info power;
|
---|
[772] | 79 | struct device_dma_parameters *dma_parms;
|
---|
[679] | 80 | struct list_head dma_pools; /* dma pools (if dma'ble) */
|
---|
| 81 | struct device_driver *driver;
|
---|
[772] | 82 | void *driver_data; /* Driver data, set and get with
|
---|
| 83 | dev_set_drvdata/dev_get_drvdata */
|
---|
[679] | 84 | struct pm_dev *pm_dev;
|
---|
| 85 | char bus_id[20];
|
---|
[772] | 86 | const struct class *class;
|
---|
[679] | 87 | spinlock_t devres_lock;
|
---|
| 88 | struct list_head devres_head;
|
---|
| 89 | const struct attribute_group **groups; /* optional groups */
|
---|
| 90 | struct dma_map_ops *dma_ops;
|
---|
| 91 | u64 *dma_mask; /* dma mask (if dma'able device) */
|
---|
| 92 | u64 coherent_dma_mask; /* Like dma_mask, but for
|
---|
| 93 | alloc_coherent mappings as
|
---|
| 94 | not all hardware supports
|
---|
| 95 | 64 bit addresses for consistent
|
---|
| 96 | allocations such descriptors. */
|
---|
| 97 | struct device_node *of_node; /* associated device tree node */
|
---|
| 98 | } device;
|
---|
[305] | 99 |
|
---|
[679] | 100 | static inline struct device *kobj_to_dev(struct kobject *kobj)
|
---|
| 101 | {
|
---|
| 102 | return container_of(kobj, struct device, kobj);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | static inline const char *dev_name(const struct device *dev)
|
---|
| 106 | {
|
---|
[772] | 107 | #if 0
|
---|
[679] | 108 | /* Use the init name until the kobject becomes available */
|
---|
| 109 | if (dev->init_name)
|
---|
| 110 | return dev->init_name;
|
---|
| 111 |
|
---|
| 112 | return kobject_name(&dev->kobj);
|
---|
[772] | 113 | #else
|
---|
| 114 | return "uniaud32";
|
---|
| 115 | #endif
|
---|
[679] | 116 | }
|
---|
| 117 |
|
---|
[305] | 118 | struct bus_type {
|
---|
[679] | 119 | const char *name;
|
---|
| 120 | const char *dev_name;
|
---|
| 121 | int (*match)(struct device *dev, struct device_driver *drv);
|
---|
[777] | 122 | int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
|
---|
[679] | 123 | int (*probe)(struct device *dev);
|
---|
| 124 | int (*remove)(struct device *dev);
|
---|
| 125 | struct subsys_private *p;
|
---|
| 126 | struct lock_class_key lock_key;
|
---|
[305] | 127 | };
|
---|
| 128 |
|
---|
| 129 | struct device_driver {
|
---|
[679] | 130 | const char *name;
|
---|
| 131 | struct bus_type *bus;
|
---|
| 132 |
|
---|
| 133 | struct module *owner;
|
---|
| 134 | const char *mod_name; /* used for built-in modules */
|
---|
| 135 |
|
---|
| 136 | bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
|
---|
| 137 |
|
---|
| 138 | const struct of_device_id *of_match_table;
|
---|
| 139 |
|
---|
| 140 | int (*probe) (struct device *dev);
|
---|
| 141 | int (*remove) (struct device *dev);
|
---|
| 142 | void (*shutdown) (struct device *dev);
|
---|
| 143 | int (*suspend) (struct device *dev, u32 state);
|
---|
| 144 | int (*resume) (struct device *dev);
|
---|
| 145 | const struct attribute_group **groups;
|
---|
| 146 |
|
---|
| 147 | const struct dev_pm_ops *pm;
|
---|
| 148 |
|
---|
| 149 | struct driver_private *p;
|
---|
[305] | 150 | };
|
---|
| 151 |
|
---|
| 152 | struct device_attribute {
|
---|
[679] | 153 | struct attribute attr;
|
---|
| 154 | ssize_t (*show)(struct device *dev, struct device_attribute *attr,
|
---|
| 155 | char *buf);
|
---|
| 156 | ssize_t (*store)(struct device *dev, struct device_attribute *attr,
|
---|
| 157 | const char *buf, size_t count);
|
---|
[305] | 158 | };
|
---|
| 159 |
|
---|
[679] | 160 | extern int __must_check driver_register(struct device_driver *drv);
|
---|
| 161 | extern void driver_unregister(struct device_driver *drv);
|
---|
| 162 | extern struct device_driver *driver_find(const char *name,
|
---|
| 163 | struct bus_type *bus);
|
---|
[305] | 164 |
|
---|
[441] | 165 | #define MODULE_ALIAS_CHARDEV_MAJOR(x)
|
---|
[305] | 166 |
|
---|
[679] | 167 | #define dev_dbg_ratelimited dev_dbg
|
---|
| 168 | #define dev_emerg dev_dbg
|
---|
| 169 | #define dev_crit dev_dbg
|
---|
| 170 | #define dev_alert dev_dbg
|
---|
| 171 | #define dev_err dev_dbg
|
---|
| 172 | #define dev_warn dev_dbg
|
---|
| 173 | #define dev_notice dev_dbg
|
---|
| 174 | #define dev_info dev_dbg
|
---|
| 175 |
|
---|
| 176 | #define dev_err_ratelimited dev_err
|
---|
| 177 |
|
---|
| 178 | #define DEVICE_ATTR_RO(_name) \
|
---|
| 179 | struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
|
---|
| 180 |
|
---|
| 181 | int dev_dbg(const struct device *dev, const char *fmt, ...);
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | /*
|
---|
| 186 | * get_device - atomically increment the reference count for the device.
|
---|
| 187 | *
|
---|
| 188 | */
|
---|
| 189 | extern struct device *get_device(struct device *dev);
|
---|
| 190 | extern void put_device(struct device *dev);
|
---|
| 191 |
|
---|
| 192 | static inline int device_add(struct device *dev) { return 0; }
|
---|
| 193 | static inline void device_del(struct device *dev) { }
|
---|
| 194 | extern void device_initialize(struct device *dev);
|
---|
| 195 | extern int dev_set_name(struct device *dev, const char *name, ...);
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | static inline int device_is_registered(struct device *dev)
|
---|
| 199 | {
|
---|
| 200 | return dev->kobj.state_in_sysfs;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | static inline void device_enable_async_suspend(struct device *dev)
|
---|
| 204 | {
|
---|
| 205 | if (!dev->power.is_prepared)
|
---|
| 206 | dev->power.async_suspend = 1;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | extern int __must_check bus_register(struct bus_type *bus);
|
---|
| 210 |
|
---|
| 211 | static inline void bus_unregister(struct bus_type *bus) {}
|
---|
| 212 | int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
|
---|
| 213 | int (*fn)(struct device *dev, void *data));
|
---|
| 214 |
|
---|
| 215 | static inline void device_lock(struct device *dev)
|
---|
| 216 | {
|
---|
| 217 | mutex_lock(&dev->mutex);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | static inline void device_unlock(struct device *dev)
|
---|
| 221 | {
|
---|
| 222 | mutex_unlock(&dev->mutex);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | extern int device_attach(struct device *dev);
|
---|
| 226 | //static inline int device_attach(struct device *dev) {return 0;}
|
---|
| 227 | extern void devres_add(struct device *dev, void *res);
|
---|
| 228 | extern int driver_attach(struct device_driver *drv);
|
---|
| 229 |
|
---|
| 230 | /* device resource management */
|
---|
| 231 | typedef void (*dr_release_t)(struct device *dev, void *res);
|
---|
| 232 | typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
|
---|
| 233 |
|
---|
| 234 | #define NUMA_NO_NODE (-1)
|
---|
[772] | 235 | void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
|
---|
| 236 | int nid, const char *name);
|
---|
| 237 | #define devres_alloc(release, size, gfp) \
|
---|
| 238 | __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
|
---|
[679] | 239 |
|
---|
| 240 | /**
|
---|
| 241 | * struct class - device classes
|
---|
| 242 | * @name: Name of the class.
|
---|
| 243 | * @owner: The module owner.
|
---|
| 244 | * @class_attrs: Default attributes of this class.
|
---|
| 245 | * @dev_attrs: Default attributes of the devices belong to the class.
|
---|
| 246 | * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
|
---|
| 247 | * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
|
---|
| 248 | * @dev_uevent: Called when a device is added, removed from this class, or a
|
---|
| 249 | * few other things that generate uevents to add the environment
|
---|
| 250 | * variables.
|
---|
| 251 | * @devnode: Callback to provide the devtmpfs.
|
---|
| 252 | * @class_release: Called to release this class.
|
---|
| 253 | * @dev_release: Called to release the device.
|
---|
| 254 | * @suspend: Used to put the device to sleep mode, usually to a low power
|
---|
| 255 | * state.
|
---|
| 256 | * @resume: Used to bring the device from the sleep mode.
|
---|
| 257 | * @ns_type: Callbacks so sysfs can detemine namespaces.
|
---|
| 258 | * @namespace: Namespace of the device belongs to this class.
|
---|
| 259 | * @pm: The default device power management operations of this class.
|
---|
| 260 | * @p: The private data of the driver core, no one other than the
|
---|
| 261 | * driver core can touch this.
|
---|
| 262 | *
|
---|
| 263 | * A class is a higher-level view of a device that abstracts out low-level
|
---|
| 264 | * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
|
---|
| 265 | * at the class level, they are all simply disks. Classes allow user space
|
---|
| 266 | * to work with devices based on what they do, rather than how they are
|
---|
| 267 | * connected or how they work.
|
---|
| 268 | */
|
---|
| 269 | struct class {
|
---|
| 270 | const char *name;
|
---|
| 271 | struct module *owner;
|
---|
| 272 |
|
---|
| 273 | struct class_attribute *class_attrs;
|
---|
| 274 | struct device_attribute *dev_attrs;
|
---|
| 275 | struct bin_attribute *dev_bin_attrs;
|
---|
| 276 | struct kobject *dev_kobj;
|
---|
| 277 |
|
---|
| 278 | int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
|
---|
| 279 | char *(*devnode)(struct device *dev, mode_t *mode);
|
---|
| 280 |
|
---|
| 281 | void (*class_release)(struct class *class);
|
---|
| 282 | void (*dev_release)(struct device *dev);
|
---|
| 283 |
|
---|
| 284 | int (*suspend)(struct device *dev, pm_message_t state);
|
---|
| 285 | int (*resume)(struct device *dev);
|
---|
| 286 |
|
---|
| 287 | const struct kobj_ns_type_operations *ns_type;
|
---|
| 288 | const void *(*namespace)(struct device *dev);
|
---|
| 289 |
|
---|
| 290 | const struct dev_pm_ops *pm;
|
---|
| 291 |
|
---|
| 292 | struct subsys_private *p;
|
---|
| 293 | };
|
---|
| 294 |
|
---|
| 295 | extern int __must_check device_bind_driver(struct device *dev);
|
---|
| 296 | int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
|
---|
| 297 | void *data, int (*fn)(struct device_driver *, void *));
|
---|
| 298 | extern void devres_free(void *res);
|
---|
| 299 | extern void *devres_find(struct device *dev, dr_release_t release,
|
---|
| 300 | dr_match_t match, void *match_data);
|
---|
| 301 |
|
---|
[772] | 302 | /* devres group */
|
---|
| 303 | void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
|
---|
| 304 | void devres_close_group(struct device *dev, void *id);
|
---|
| 305 | void devres_remove_group(struct device *dev, void *id);
|
---|
| 306 | int devres_release_group(struct device *dev, void *id);
|
---|
| 307 |
|
---|
[679] | 308 | /* debugging and troubleshooting/diagnostic helpers. */
|
---|
| 309 | extern const char *dev_driver_string(const struct device *dev);
|
---|
[772] | 310 | void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
|
---|
[679] | 311 |
|
---|
[772] | 312 | static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
|
---|
| 313 | {
|
---|
| 314 | return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
|
---|
| 315 | }
|
---|
[717] | 316 |
|
---|
[772] | 317 | static inline void *devm_kmalloc_array(struct device *dev,
|
---|
| 318 | size_t n, size_t size, gfp_t flags)
|
---|
| 319 | {
|
---|
| 320 | // size_t bytes;
|
---|
| 321 |
|
---|
| 322 | // if (unlikely(check_mul_overflow(n, size, &bytes)))
|
---|
| 323 | // return NULL;
|
---|
| 324 |
|
---|
| 325 | return devm_kmalloc(dev, n * size, flags);
|
---|
| 326 | }
|
---|
| 327 | static inline void *devm_kcalloc(struct device *dev,
|
---|
| 328 | size_t n, size_t size, gfp_t flags)
|
---|
| 329 | {
|
---|
| 330 | return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[717] | 333 | /* allows to add/remove a custom action to devres stack */
|
---|
| 334 | int devm_add_action(struct device *dev, void (*action)(void *), void *data);
|
---|
| 335 | void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
|
---|
[772] | 336 |
|
---|
| 337 | static inline int dev_to_node(struct device *dev)
|
---|
| 338 | {
|
---|
| 339 | return NUMA_NO_NODE;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | static inline void *dev_get_drvdata(const struct device *dev)
|
---|
| 343 | {
|
---|
| 344 | return dev->driver_data;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | static inline void dev_set_drvdata(struct device *dev, void *data)
|
---|
| 348 | {
|
---|
| 349 | dev->driver_data = data;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /* Generic device matching functions that all busses can use to match with */
|
---|
| 353 | int device_match_name(struct device *dev, const void *name);
|
---|
| 354 | int device_match_of_node(struct device *dev, const void *np);
|
---|
| 355 |
|
---|
| 356 | char *devm_kasprintf(struct device *dev, gfp_t gfp,
|
---|
| 357 | const char *fmt, ...);
|
---|
| 358 |
|
---|
[305] | 359 | #endif /* _LINUX_DEVICE_H */
|
---|
| 360 |
|
---|