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