]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/include/linux/device.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / include / linux / device.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef _LINUX_DEVICE_H_
32 #define _LINUX_DEVICE_H_
33
34 #include <linux/err.h>
35 #include <linux/types.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/list.h>
39 #include <linux/compiler.h>
40 #include <linux/types.h>
41 #include <linux/module.h>
42 #include <linux/workqueue.h>
43 #include <linux/kdev_t.h>
44 #include <asm/atomic.h>
45
46 #include <sys/bus.h>
47
48 struct device;
49 struct fwnode_handle;
50
51 struct class {
52         const char      *name;
53         struct module   *owner;
54         struct kobject  kobj;
55         devclass_t      bsdclass;
56         const struct dev_pm_ops *pm;
57         const struct attribute_group **dev_groups;
58         void            (*class_release)(struct class *class);
59         void            (*dev_release)(struct device *dev);
60         char *          (*devnode)(struct device *dev, umode_t *mode);
61 };
62
63 struct dev_pm_ops {
64         int (*prepare)(struct device *dev);
65         int (*suspend)(struct device *dev);
66         int (*suspend_late)(struct device *dev);
67         int (*resume)(struct device *dev);
68         int (*resume_early)(struct device *dev);
69         int (*freeze)(struct device *dev);
70         int (*freeze_late)(struct device *dev);
71         int (*thaw)(struct device *dev);
72         int (*thaw_early)(struct device *dev);
73         int (*poweroff)(struct device *dev);
74         int (*poweroff_late)(struct device *dev);
75         int (*restore)(struct device *dev);
76         int (*restore_early)(struct device *dev);
77         int (*runtime_suspend)(struct device *dev);
78         int (*runtime_resume)(struct device *dev);
79         int (*runtime_idle)(struct device *dev);
80 };
81
82 struct device_driver {
83         const char      *name;
84         const struct dev_pm_ops *pm;
85 };
86
87 struct device_type {
88         const char      *name;
89 };
90
91 struct device {
92         struct device   *parent;
93         struct list_head irqents;
94         device_t        bsddev;
95         /*
96          * The following flag is used to determine if the LinuxKPI is
97          * responsible for detaching the BSD device or not. If the
98          * LinuxKPI got the BSD device using devclass_get_device(), it
99          * must not try to detach or delete it, because it's already
100          * done somewhere else.
101          */
102         bool            bsddev_attached_here;
103         struct device_driver *driver;
104         struct device_type *type;
105         dev_t           devt;
106         struct class    *class;
107         void            (*release)(struct device *dev);
108         struct kobject  kobj;
109         void            *dma_priv;
110         void            *driver_data;
111         unsigned int    irq;
112 #define LINUX_IRQ_INVALID       65535
113         unsigned int    irq_start;
114         unsigned int    irq_end;
115         const struct attribute_group **groups;
116         struct fwnode_handle *fwnode;
117
118         spinlock_t      devres_lock;
119         struct list_head devres_head;
120 };
121
122 extern struct device linux_root_device;
123 extern struct kobject linux_class_root;
124 extern const struct kobj_type linux_dev_ktype;
125 extern const struct kobj_type linux_class_ktype;
126
127 struct class_attribute {
128         struct attribute attr;
129         ssize_t (*show)(struct class *, struct class_attribute *, char *);
130         ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
131         const void *(*namespace)(struct class *, const struct class_attribute *);
132 };
133
134 #define CLASS_ATTR(_name, _mode, _show, _store)                         \
135         struct class_attribute class_attr_##_name =                     \
136             { { #_name, NULL, _mode }, _show, _store }
137
138 struct device_attribute {
139         struct attribute        attr;
140         ssize_t                 (*show)(struct device *,
141                                         struct device_attribute *, char *);
142         ssize_t                 (*store)(struct device *,
143                                         struct device_attribute *, const char *,
144                                         size_t);
145 };
146
147 #define DEVICE_ATTR(_name, _mode, _show, _store)                        \
148         struct device_attribute dev_attr_##_name =                      \
149             __ATTR(_name, _mode, _show, _store)
150 #define DEVICE_ATTR_RO(_name)                                           \
151         struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
152 #define DEVICE_ATTR_WO(_name)                                           \
153         struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
154 #define DEVICE_ATTR_RW(_name)                                           \
155         struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
156
157 /* Simple class attribute that is just a static string */
158 struct class_attribute_string {
159         struct class_attribute attr;
160         char *str;
161 };
162
163 static inline ssize_t
164 show_class_attr_string(struct class *class,
165                                 struct class_attribute *attr, char *buf)
166 {
167         struct class_attribute_string *cs;
168         cs = container_of(attr, struct class_attribute_string, attr);
169         return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
170 }
171
172 /* Currently read-only only */
173 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
174         { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
175 #define CLASS_ATTR_STRING(_name, _mode, _str) \
176         struct class_attribute_string class_attr_##_name = \
177                 _CLASS_ATTR_STRING(_name, _mode, _str)
178
179 #define dev_err(dev, fmt, ...)  device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
180 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
181 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
182 #define dev_notice(dev, fmt, ...)       device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
183 #define dev_dbg(dev, fmt, ...)  do { } while (0)
184 #define dev_printk(lvl, dev, fmt, ...)                                  \
185             device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
186
187 #define dev_err_once(dev, ...) do {             \
188         static bool __dev_err_once;             \
189         if (!__dev_err_once) {                  \
190                 __dev_err_once = 1;             \
191                 dev_err(dev, __VA_ARGS__);      \
192         }                                       \
193 } while (0)
194
195 #define dev_err_ratelimited(dev, ...) do {      \
196         static linux_ratelimit_t __ratelimited; \
197         if (linux_ratelimited(&__ratelimited))  \
198                 dev_err(dev, __VA_ARGS__);      \
199 } while (0)
200
201 #define dev_warn_ratelimited(dev, ...) do {     \
202         static linux_ratelimit_t __ratelimited; \
203         if (linux_ratelimited(&__ratelimited))  \
204                 dev_warn(dev, __VA_ARGS__);     \
205 } while (0)
206
207 static inline void *
208 dev_get_drvdata(const struct device *dev)
209 {
210
211         return dev->driver_data;
212 }
213
214 static inline void
215 dev_set_drvdata(struct device *dev, void *data)
216 {
217
218         dev->driver_data = data;
219 }
220
221 static inline struct device *
222 get_device(struct device *dev)
223 {
224
225         if (dev)
226                 kobject_get(&dev->kobj);
227
228         return (dev);
229 }
230
231 static inline char *
232 dev_name(const struct device *dev)
233 {
234
235         return kobject_name(&dev->kobj);
236 }
237
238 #define dev_set_name(_dev, _fmt, ...)                                   \
239         kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
240
241 static inline void
242 put_device(struct device *dev)
243 {
244
245         if (dev)
246                 kobject_put(&dev->kobj);
247 }
248
249 static inline int
250 class_register(struct class *class)
251 {
252
253         class->bsdclass = devclass_create(class->name);
254         kobject_init(&class->kobj, &linux_class_ktype);
255         kobject_set_name(&class->kobj, class->name);
256         kobject_add(&class->kobj, &linux_class_root, class->name);
257
258         return (0);
259 }
260
261 static inline void
262 class_unregister(struct class *class)
263 {
264
265         kobject_put(&class->kobj);
266 }
267
268 static inline struct device *kobj_to_dev(struct kobject *kobj)
269 {
270         return container_of(kobj, struct device, kobj);
271 }
272
273 /*
274  * Devices are registered and created for exporting to sysfs. Create
275  * implies register and register assumes the device fields have been
276  * setup appropriately before being called.
277  */
278 static inline void
279 device_initialize(struct device *dev)
280 {
281         device_t bsddev = NULL;
282         int unit = -1;
283
284         if (dev->devt) {
285                 unit = MINOR(dev->devt);
286                 bsddev = devclass_get_device(dev->class->bsdclass, unit);
287                 dev->bsddev_attached_here = false;
288         } else if (dev->parent == NULL) {
289                 bsddev = devclass_get_device(dev->class->bsdclass, 0);
290                 dev->bsddev_attached_here = false;
291         } else {
292                 dev->bsddev_attached_here = true;
293         }
294
295         if (bsddev == NULL && dev->parent != NULL) {
296                 bsddev = device_add_child(dev->parent->bsddev,
297                     dev->class->kobj.name, unit);
298         }
299
300         if (bsddev != NULL)
301                 device_set_softc(bsddev, dev);
302
303         dev->bsddev = bsddev;
304         MPASS(dev->bsddev != NULL);
305         kobject_init(&dev->kobj, &linux_dev_ktype);
306
307         spin_lock_init(&dev->devres_lock);
308         INIT_LIST_HEAD(&dev->devres_head);
309 }
310
311 static inline int
312 device_add(struct device *dev)
313 {
314         if (dev->bsddev != NULL) {
315                 if (dev->devt == 0)
316                         dev->devt = makedev(0, device_get_unit(dev->bsddev));
317         }
318         kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
319
320         if (dev->groups)
321                 return (sysfs_create_groups(&dev->kobj, dev->groups));
322
323         return (0);
324 }
325
326 static inline void
327 device_create_release(struct device *dev)
328 {
329         kfree(dev);
330 }
331
332 static inline struct device *
333 device_create_groups_vargs(struct class *class, struct device *parent,
334     dev_t devt, void *drvdata, const struct attribute_group **groups,
335     const char *fmt, va_list args)
336 {
337         struct device *dev = NULL;
338         int retval = -ENODEV;
339
340         if (class == NULL || IS_ERR(class))
341                 goto error;
342
343         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
344         if (!dev) {
345                 retval = -ENOMEM;
346                 goto error;
347         }
348
349         dev->devt = devt;
350         dev->class = class;
351         dev->parent = parent;
352         dev->groups = groups;
353         dev->release = device_create_release;
354         /* device_initialize() needs the class and parent to be set */
355         device_initialize(dev);
356         dev_set_drvdata(dev, drvdata);
357
358         retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
359         if (retval)
360                 goto error;
361
362         retval = device_add(dev);
363         if (retval)
364                 goto error;
365
366         return dev;
367
368 error:
369         put_device(dev);
370         return ERR_PTR(retval);
371 }
372
373 static inline struct device *
374 device_create_with_groups(struct class *class,
375     struct device *parent, dev_t devt, void *drvdata,
376     const struct attribute_group **groups, const char *fmt, ...)
377 {
378         va_list vargs;
379         struct device *dev;
380
381         va_start(vargs, fmt);
382         dev = device_create_groups_vargs(class, parent, devt, drvdata,
383             groups, fmt, vargs);
384         va_end(vargs);
385         return dev;
386 }
387
388 static inline bool
389 device_is_registered(struct device *dev)
390 {
391
392         return (dev->bsddev != NULL);
393 }
394
395 static inline int
396 device_register(struct device *dev)
397 {
398         device_t bsddev = NULL;
399         int unit = -1;
400
401         if (device_is_registered(dev))
402                 goto done;
403
404         if (dev->devt) {
405                 unit = MINOR(dev->devt);
406                 bsddev = devclass_get_device(dev->class->bsdclass, unit);
407                 dev->bsddev_attached_here = false;
408         } else if (dev->parent == NULL) {
409                 bsddev = devclass_get_device(dev->class->bsdclass, 0);
410                 dev->bsddev_attached_here = false;
411         } else {
412                 dev->bsddev_attached_here = true;
413         }
414         if (bsddev == NULL && dev->parent != NULL) {
415                 bsddev = device_add_child(dev->parent->bsddev,
416                     dev->class->kobj.name, unit);
417         }
418         if (bsddev != NULL) {
419                 if (dev->devt == 0)
420                         dev->devt = makedev(0, device_get_unit(bsddev));
421                 device_set_softc(bsddev, dev);
422         }
423         dev->bsddev = bsddev;
424 done:
425         kobject_init(&dev->kobj, &linux_dev_ktype);
426         kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
427
428         sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
429
430         return (0);
431 }
432
433 static inline void
434 device_unregister(struct device *dev)
435 {
436         device_t bsddev;
437
438         sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
439
440         bsddev = dev->bsddev;
441         dev->bsddev = NULL;
442
443         if (bsddev != NULL && dev->bsddev_attached_here) {
444                 mtx_lock(&Giant);
445                 device_delete_child(device_get_parent(bsddev), bsddev);
446                 mtx_unlock(&Giant);
447         }
448         put_device(dev);
449 }
450
451 static inline void
452 device_del(struct device *dev)
453 {
454         device_t bsddev;
455
456         bsddev = dev->bsddev;
457         dev->bsddev = NULL;
458
459         if (bsddev != NULL && dev->bsddev_attached_here) {
460                 mtx_lock(&Giant);
461                 device_delete_child(device_get_parent(bsddev), bsddev);
462                 mtx_unlock(&Giant);
463         }
464 }
465
466 struct device *device_create(struct class *class, struct device *parent,
467             dev_t devt, void *drvdata, const char *fmt, ...);
468
469 static inline void
470 device_destroy(struct class *class, dev_t devt)
471 {
472         device_t bsddev;
473         int unit;
474
475         unit = MINOR(devt);
476         bsddev = devclass_get_device(class->bsdclass, unit);
477         if (bsddev != NULL)
478                 device_unregister(device_get_softc(bsddev));
479 }
480
481 #define dev_pm_set_driver_flags(dev, flags) do { \
482 } while (0)
483
484 static inline void
485 linux_class_kfree(struct class *class)
486 {
487
488         kfree(class);
489 }
490
491 static inline struct class *
492 class_create(struct module *owner, const char *name)
493 {
494         struct class *class;
495         int error;
496
497         class = kzalloc(sizeof(*class), M_WAITOK);
498         class->owner = owner;
499         class->name = name;
500         class->class_release = linux_class_kfree;
501         error = class_register(class);
502         if (error) {
503                 kfree(class);
504                 return (NULL);
505         }
506
507         return (class);
508 }
509
510 static inline void
511 class_destroy(struct class *class)
512 {
513
514         if (class == NULL)
515                 return;
516         class_unregister(class);
517 }
518
519 static inline int
520 device_create_file(struct device *dev, const struct device_attribute *attr)
521 {
522
523         if (dev)
524                 return sysfs_create_file(&dev->kobj, &attr->attr);
525         return -EINVAL;
526 }
527
528 static inline void
529 device_remove_file(struct device *dev, const struct device_attribute *attr)
530 {
531
532         if (dev)
533                 sysfs_remove_file(&dev->kobj, &attr->attr);
534 }
535
536 static inline int
537 class_create_file(struct class *class, const struct class_attribute *attr)
538 {
539
540         if (class)
541                 return sysfs_create_file(&class->kobj, &attr->attr);
542         return -EINVAL;
543 }
544
545 static inline void
546 class_remove_file(struct class *class, const struct class_attribute *attr)
547 {
548
549         if (class)
550                 sysfs_remove_file(&class->kobj, &attr->attr);
551 }
552
553 static inline int
554 dev_to_node(struct device *dev)
555 {
556         return -1;
557 }
558
559 char *kvasprintf(gfp_t, const char *, va_list);
560 char *kasprintf(gfp_t, const char *, ...);
561
562 #endif  /* _LINUX_DEVICE_H_ */