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: stable/12/sys/compat/linuxkpi/common/include/linux/device.h 369508 2021-03-23 11:48:48Z hselasky $
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 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 50000
65 int (*prepare)(struct device *dev);
66 #endif
67 int (*suspend)(struct device *dev);
68 int (*suspend_late)(struct device *dev);
69 int (*resume)(struct device *dev);
70 int (*resume_early)(struct device *dev);
71 int (*freeze)(struct device *dev);
72 int (*freeze_late)(struct device *dev);
73 int (*thaw)(struct device *dev);
74 int (*thaw_early)(struct device *dev);
75 int (*poweroff)(struct device *dev);
76 int (*poweroff_late)(struct device *dev);
77 int (*restore)(struct device *dev);
78 int (*restore_early)(struct device *dev);
79 int (*runtime_suspend)(struct device *dev);
80 int (*runtime_resume)(struct device *dev);
81 int (*runtime_idle)(struct device *dev);
82 };
83
84 struct device_driver {
85 const char *name;
86 const struct dev_pm_ops *pm;
87 };
88
89 struct device_type {
90 const char *name;
91 };
92
93 struct device {
94 struct device *parent;
95 struct list_head irqents;
96 device_t bsddev;
97 /*
98 * The following flag is used to determine if the LinuxKPI is
99 * responsible for detaching the BSD device or not. If the
100 * LinuxKPI got the BSD device using devclass_get_device(), it
101 * must not try to detach or delete it, because it's already
102 * done somewhere else.
103 */
104 bool bsddev_attached_here;
105 struct device_driver *driver;
106 struct device_type *type;
107 dev_t devt;
108 struct class *class;
109 void (*release)(struct device *dev);
110 struct kobject kobj;
111 union {
112 const u64 *dma_mask; /* XXX for backwards compat */
113 void *dma_priv;
114 };
115 void *driver_data;
116 unsigned int irq;
117 #define LINUX_IRQ_INVALID 65535
118 unsigned int irq_start;
119 unsigned int irq_end;
120 const struct attribute_group **groups;
121 struct fwnode_handle *fwnode;
122
123 spinlock_t devres_lock;
124 struct list_head devres_head;
125 };
126
127 extern struct device linux_root_device;
128 extern struct kobject linux_class_root;
129 extern const struct kobj_type linux_dev_ktype;
130 extern const struct kobj_type linux_class_ktype;
131
132 struct class_attribute {
133 struct attribute attr;
134 ssize_t (*show)(struct class *, struct class_attribute *, char *);
135 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
136 const void *(*namespace)(struct class *, const struct class_attribute *);
137 };
138
139 #define CLASS_ATTR(_name, _mode, _show, _store) \
140 struct class_attribute class_attr_##_name = \
141 { { #_name, NULL, _mode }, _show, _store }
142
143 struct device_attribute {
144 struct attribute attr;
145 ssize_t (*show)(struct device *,
146 struct device_attribute *, char *);
147 ssize_t (*store)(struct device *,
148 struct device_attribute *, const char *,
149 size_t);
150 };
151
152 #define DEVICE_ATTR(_name, _mode, _show, _store) \
153 struct device_attribute dev_attr_##_name = \
154 __ATTR(_name, _mode, _show, _store)
155 #define DEVICE_ATTR_RO(_name) \
156 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
157 #define DEVICE_ATTR_WO(_name) \
158 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
159 #define DEVICE_ATTR_RW(_name) \
160 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
161
162 /* Simple class attribute that is just a static string */
163 struct class_attribute_string {
164 struct class_attribute attr;
165 char *str;
166 };
167
168 static inline ssize_t
show_class_attr_string(struct class * class,struct class_attribute * attr,char * buf)169 show_class_attr_string(struct class *class,
170 struct class_attribute *attr, char *buf)
171 {
172 struct class_attribute_string *cs;
173 cs = container_of(attr, struct class_attribute_string, attr);
174 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
175 }
176
177 /* Currently read-only only */
178 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
179 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
180 #define CLASS_ATTR_STRING(_name, _mode, _str) \
181 struct class_attribute_string class_attr_##_name = \
182 _CLASS_ATTR_STRING(_name, _mode, _str)
183
184 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
185 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
186 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
187 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
188 #define dev_dbg(dev, fmt, ...) do { } while (0)
189 #define dev_printk(lvl, dev, fmt, ...) \
190 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
191
192 #define dev_err_once(dev, ...) do { \
193 static bool __dev_err_once; \
194 if (!__dev_err_once) { \
195 __dev_err_once = 1; \
196 dev_err(dev, __VA_ARGS__); \
197 } \
198 } while (0)
199
200 #define dev_err_ratelimited(dev, ...) do { \
201 static linux_ratelimit_t __ratelimited; \
202 if (linux_ratelimited(&__ratelimited)) \
203 dev_err(dev, __VA_ARGS__); \
204 } while (0)
205
206 #define dev_warn_ratelimited(dev, ...) do { \
207 static linux_ratelimit_t __ratelimited; \
208 if (linux_ratelimited(&__ratelimited)) \
209 dev_warn(dev, __VA_ARGS__); \
210 } while (0)
211
212 static inline void *
dev_get_drvdata(const struct device * dev)213 dev_get_drvdata(const struct device *dev)
214 {
215
216 return dev->driver_data;
217 }
218
219 static inline void
dev_set_drvdata(struct device * dev,void * data)220 dev_set_drvdata(struct device *dev, void *data)
221 {
222
223 dev->driver_data = data;
224 }
225
226 static inline struct device *
get_device(struct device * dev)227 get_device(struct device *dev)
228 {
229
230 if (dev)
231 kobject_get(&dev->kobj);
232
233 return (dev);
234 }
235
236 static inline char *
dev_name(const struct device * dev)237 dev_name(const struct device *dev)
238 {
239
240 return kobject_name(&dev->kobj);
241 }
242
243 #define dev_set_name(_dev, _fmt, ...) \
244 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
245
246 static inline void
put_device(struct device * dev)247 put_device(struct device *dev)
248 {
249
250 if (dev)
251 kobject_put(&dev->kobj);
252 }
253
254 static inline int
class_register(struct class * class)255 class_register(struct class *class)
256 {
257
258 class->bsdclass = devclass_create(class->name);
259 kobject_init(&class->kobj, &linux_class_ktype);
260 kobject_set_name(&class->kobj, class->name);
261 kobject_add(&class->kobj, &linux_class_root, class->name);
262
263 return (0);
264 }
265
266 static inline void
class_unregister(struct class * class)267 class_unregister(struct class *class)
268 {
269
270 kobject_put(&class->kobj);
271 }
272
kobj_to_dev(struct kobject * kobj)273 static inline struct device *kobj_to_dev(struct kobject *kobj)
274 {
275 return container_of(kobj, struct device, kobj);
276 }
277
278 /*
279 * Devices are registered and created for exporting to sysfs. Create
280 * implies register and register assumes the device fields have been
281 * setup appropriately before being called.
282 */
283 static inline void
device_initialize(struct device * dev)284 device_initialize(struct device *dev)
285 {
286 device_t bsddev = NULL;
287 int unit = -1;
288
289 if (dev->devt) {
290 unit = MINOR(dev->devt);
291 bsddev = devclass_get_device(dev->class->bsdclass, unit);
292 dev->bsddev_attached_here = false;
293 } else if (dev->parent == NULL) {
294 bsddev = devclass_get_device(dev->class->bsdclass, 0);
295 dev->bsddev_attached_here = false;
296 } else {
297 dev->bsddev_attached_here = true;
298 }
299
300 if (bsddev == NULL && dev->parent != NULL) {
301 bsddev = device_add_child(dev->parent->bsddev,
302 dev->class->kobj.name, unit);
303 }
304
305 if (bsddev != NULL)
306 device_set_softc(bsddev, dev);
307
308 dev->bsddev = bsddev;
309 MPASS(dev->bsddev != NULL);
310 kobject_init(&dev->kobj, &linux_dev_ktype);
311
312 spin_lock_init(&dev->devres_lock);
313 INIT_LIST_HEAD(&dev->devres_head);
314 }
315
316 static inline int
device_add(struct device * dev)317 device_add(struct device *dev)
318 {
319 if (dev->bsddev != NULL) {
320 if (dev->devt == 0)
321 dev->devt = makedev(0, device_get_unit(dev->bsddev));
322 }
323 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
324
325 if (dev->groups)
326 return (sysfs_create_groups(&dev->kobj, dev->groups));
327
328 return (0);
329 }
330
331 static inline void
device_create_release(struct device * dev)332 device_create_release(struct device *dev)
333 {
334 kfree(dev);
335 }
336
337 static inline struct device *
device_create_groups_vargs(struct class * class,struct device * parent,dev_t devt,void * drvdata,const struct attribute_group ** groups,const char * fmt,va_list args)338 device_create_groups_vargs(struct class *class, struct device *parent,
339 dev_t devt, void *drvdata, const struct attribute_group **groups,
340 const char *fmt, va_list args)
341 {
342 struct device *dev = NULL;
343 int retval = -ENODEV;
344
345 if (class == NULL || IS_ERR(class))
346 goto error;
347
348 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
349 if (!dev) {
350 retval = -ENOMEM;
351 goto error;
352 }
353
354 dev->devt = devt;
355 dev->class = class;
356 dev->parent = parent;
357 dev->groups = groups;
358 dev->release = device_create_release;
359 /* device_initialize() needs the class and parent to be set */
360 device_initialize(dev);
361 dev_set_drvdata(dev, drvdata);
362
363 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
364 if (retval)
365 goto error;
366
367 retval = device_add(dev);
368 if (retval)
369 goto error;
370
371 return dev;
372
373 error:
374 put_device(dev);
375 return ERR_PTR(retval);
376 }
377
378 static inline struct device *
device_create_with_groups(struct class * class,struct device * parent,dev_t devt,void * drvdata,const struct attribute_group ** groups,const char * fmt,...)379 device_create_with_groups(struct class *class,
380 struct device *parent, dev_t devt, void *drvdata,
381 const struct attribute_group **groups, const char *fmt, ...)
382 {
383 va_list vargs;
384 struct device *dev;
385
386 va_start(vargs, fmt);
387 dev = device_create_groups_vargs(class, parent, devt, drvdata,
388 groups, fmt, vargs);
389 va_end(vargs);
390 return dev;
391 }
392
393 static inline bool
device_is_registered(struct device * dev)394 device_is_registered(struct device *dev)
395 {
396
397 return (dev->bsddev != NULL);
398 }
399
400 static inline int
device_register(struct device * dev)401 device_register(struct device *dev)
402 {
403 device_t bsddev = NULL;
404 int unit = -1;
405
406 if (device_is_registered(dev))
407 goto done;
408
409 if (dev->devt) {
410 unit = MINOR(dev->devt);
411 bsddev = devclass_get_device(dev->class->bsdclass, unit);
412 dev->bsddev_attached_here = false;
413 } else if (dev->parent == NULL) {
414 bsddev = devclass_get_device(dev->class->bsdclass, 0);
415 dev->bsddev_attached_here = false;
416 } else {
417 dev->bsddev_attached_here = true;
418 }
419 if (bsddev == NULL && dev->parent != NULL) {
420 bsddev = device_add_child(dev->parent->bsddev,
421 dev->class->kobj.name, unit);
422 }
423 if (bsddev != NULL) {
424 if (dev->devt == 0)
425 dev->devt = makedev(0, device_get_unit(bsddev));
426 device_set_softc(bsddev, dev);
427 }
428 dev->bsddev = bsddev;
429 done:
430 kobject_init(&dev->kobj, &linux_dev_ktype);
431 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
432
433 sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
434
435 return (0);
436 }
437
438 static inline void
device_unregister(struct device * dev)439 device_unregister(struct device *dev)
440 {
441 device_t bsddev;
442
443 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
444
445 bsddev = dev->bsddev;
446 dev->bsddev = NULL;
447
448 if (bsddev != NULL && dev->bsddev_attached_here) {
449 mtx_lock(&Giant);
450 device_delete_child(device_get_parent(bsddev), bsddev);
451 mtx_unlock(&Giant);
452 }
453 put_device(dev);
454 }
455
456 static inline void
device_del(struct device * dev)457 device_del(struct device *dev)
458 {
459 device_t bsddev;
460
461 bsddev = dev->bsddev;
462 dev->bsddev = NULL;
463
464 if (bsddev != NULL && dev->bsddev_attached_here) {
465 mtx_lock(&Giant);
466 device_delete_child(device_get_parent(bsddev), bsddev);
467 mtx_unlock(&Giant);
468 }
469 }
470
471 struct device *device_create(struct class *class, struct device *parent,
472 dev_t devt, void *drvdata, const char *fmt, ...);
473
474 static inline void
device_destroy(struct class * class,dev_t devt)475 device_destroy(struct class *class, dev_t devt)
476 {
477 device_t bsddev;
478 int unit;
479
480 unit = MINOR(devt);
481 bsddev = devclass_get_device(class->bsdclass, unit);
482 if (bsddev != NULL)
483 device_unregister(device_get_softc(bsddev));
484 }
485
486 #define dev_pm_set_driver_flags(dev, flags) do { \
487 } while (0)
488
489 static inline void
linux_class_kfree(struct class * class)490 linux_class_kfree(struct class *class)
491 {
492
493 kfree(class);
494 }
495
496 static inline struct class *
class_create(struct module * owner,const char * name)497 class_create(struct module *owner, const char *name)
498 {
499 struct class *class;
500 int error;
501
502 class = kzalloc(sizeof(*class), M_WAITOK);
503 class->owner = owner;
504 class->name = name;
505 class->class_release = linux_class_kfree;
506 error = class_register(class);
507 if (error) {
508 kfree(class);
509 return (NULL);
510 }
511
512 return (class);
513 }
514
515 static inline void
class_destroy(struct class * class)516 class_destroy(struct class *class)
517 {
518
519 if (class == NULL)
520 return;
521 class_unregister(class);
522 }
523
524 static inline int
device_create_file(struct device * dev,const struct device_attribute * attr)525 device_create_file(struct device *dev, const struct device_attribute *attr)
526 {
527
528 if (dev)
529 return sysfs_create_file(&dev->kobj, &attr->attr);
530 return -EINVAL;
531 }
532
533 static inline void
device_remove_file(struct device * dev,const struct device_attribute * attr)534 device_remove_file(struct device *dev, const struct device_attribute *attr)
535 {
536
537 if (dev)
538 sysfs_remove_file(&dev->kobj, &attr->attr);
539 }
540
541 static inline int
class_create_file(struct class * class,const struct class_attribute * attr)542 class_create_file(struct class *class, const struct class_attribute *attr)
543 {
544
545 if (class)
546 return sysfs_create_file(&class->kobj, &attr->attr);
547 return -EINVAL;
548 }
549
550 static inline void
class_remove_file(struct class * class,const struct class_attribute * attr)551 class_remove_file(struct class *class, const struct class_attribute *attr)
552 {
553
554 if (class)
555 sysfs_remove_file(&class->kobj, &attr->attr);
556 }
557
558 #define dev_to_node(dev) linux_dev_to_node(dev)
559 #define of_node_to_nid(node) -1
560 int linux_dev_to_node(struct device *);
561
562 char *kvasprintf(gfp_t, const char *, va_list);
563 char *kasprintf(gfp_t, const char *, ...);
564
565 #endif /* _LINUX_DEVICE_H_ */
566